MSUK1 Posted March 13, 2012 Share Posted March 13, 2012 hello, i have read a lot of articles online about mod_rewrite and have decided to use it for a new project of mine. Good news is, it works & doesnt work the code i have written wonder if anyone could just double check it for me? If i have a file.. www.example.com/member.php I would like to have the mod_rewrite function remove the .php but it only seems to remove the .php from 2nd level files (domain.com/folder/file.php -> domain.com/folder/file/) so i am using the following: RewriteEngine On RewriteBase / RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L] all help appreciated, thank you Quote Link to comment https://forums.phpfreaks.com/topic/258867-mod_rewrite-question/ Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 it does this because in the pattern it is looking for a forward slash, which will only occur in pages inside sub-folders. RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} \.php -f RewriteRule ^(.*)$ $1.php Quote Link to comment https://forums.phpfreaks.com/topic/258867-mod_rewrite-question/#findComment-1327077 Share on other sites More sharing options...
MSUK1 Posted March 14, 2012 Author Share Posted March 14, 2012 Thank you for your quick reply i also found this on the web which could be of good use to extend this idea RewriteEngine On RewriteBase / # remove .php; use THE_REQUEST to prevent infinite loops RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP RewriteRule (.*)\.php$ $1 [R=301] # remove index RewriteRule (.*)/index$ $1/ [R=301] # remove slash if not directory RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} /$ RewriteRule (.*)/ $1 [R=301] # add .php to access file, but don't redirect RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] Quote Link to comment https://forums.phpfreaks.com/topic/258867-mod_rewrite-question/#findComment-1327081 Share on other sites More sharing options...
MSUK1 Posted March 14, 2012 Author Share Posted March 14, 2012 it does this because in the pattern it is looking for a forward slash, which will only occur in pages inside sub-folders. RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} \.php -f RewriteRule ^(.*)$ $1.php I just quickly tried this method, and got a 500 internal server error -- Taking this further, how can i use the second method i posted whilst keeping the server set to "no indexing" when i enabled this in cpanel i again got another 500 internal server error for this: RewriteEngine On RewriteBase / # remove .php; use THE_REQUEST to prevent infinite loops RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP RewriteRule (.*)\.php$ $1 [R=301] # remove index RewriteRule (.*)/index$ $1/ [R=301] # remove slash if not directory RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} /$ RewriteRule (.*)/ $1 [R=301] # add .php to access file, but don't redirect RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L][color=red][b]Options -Indexes[/b][/color] Quote Link to comment https://forums.phpfreaks.com/topic/258867-mod_rewrite-question/#findComment-1327115 Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 Well, I know that the code I posted wont trigger a 500 error by itself, because I have used this exact code on a live server. Something else is causing the error. Just add the "no indexing" code to the .htaccess file. Quote Link to comment https://forums.phpfreaks.com/topic/258867-mod_rewrite-question/#findComment-1327181 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.