Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use mod_rewrite to remove .php extension and clean GET urls concurrently

This is what I've tried till now:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

RewriteCond %{QUERY_STRING} ^id=([0-9]{1})$
RewriteRule ^article\.php$ /article/%1 [L]

Basically, the first set of rules converts the URLs from something.php to something.

The second set of rules is supposed to replace anything that has article.php?id=NUMBER in it into /article/NUMBER.

Apache reports:

AH00124: Request exceeded the limit of 10 internal redirects due to probable 
configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary.
like image 981
hytromo Avatar asked Nov 07 '13 23:11

hytromo


1 Answers

The second set of rules is supposed to replace anything that has article.php?id=NUMBER in it into /article/NUMBER.

I believe you have rules reversed.

Try this code instead:

RewriteEngine On
RewriteBase /mellori/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /article\.php\?id=([^\s&]+) [NC]
RewriteRule ^ article/%1? [R=302,L]

# internally rewrites /article/123 to article.php?id=123
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,NC,QSA]

# PHP hiding rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
like image 56
anubhava Avatar answered Oct 15 '22 20:10

anubhava