Mod Rewrite is a simple question
This is my current .htaccess
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /product/
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ ./$1/ [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^contact/?$ ./contact.php [QSA,L]
Everything works fine now.
- / product / contact back to / product / contact /
- /product/contact.php back to / product / contact /
Question..
- How to make all my .php be / contact // help // faq /
- Now I have to add contact.php help.php faq.php to htaccess
- I tried adding RewriteRule ^ (. *) / $$ 1.php [QSA, L], but it will be a return loop.
Let me know how to fix it;)
a source to share
Unfortunately, this will do an infinite loop in your rewrite operations, as Apache will try to rewrite help/
to help.php
and then help.php
to help/
, etc. etc.
If you really need it, do it in your application (example in PHP):
<?php
// The (\?.*)? so it "ignores" query strings
if (preg_match('/\.php(\?.*)?$/', $_SERVER['REQUEST_URI'])) {
header('Location: ' . str_replace('.php', '/', $_SERVER['REQUEST_URI']));
exit;
}
a source to share
Is it possible that this line is reversed?
RewriteRule ^(.*)/$ $1.php [L,QSA]
As I understand it, this overwrites domain.com/product/help/ to domain.com/product/help.php. Try
RewriteRule ^(.*).php $1/ [L,QSA]
This should overwrite domain.com/product/help.php to domain.com/product/help/ and domain.com/foobar/baz.php to .com / foobar / baz / etc.
a source to share