Use mod_rewrite to remove the .php extension and clear the GET URLs at the same time

This is what I have tried so far:

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 translates URLs from something.php into something.

Is the second set of rules supposed to replace everything that the article.php file has? id = NUMBER in / 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. 
+6
source share
2 answers

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

I believe that you have changed the rules.

Try using this code:

 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] 
+2
source

You need to make sure that when matching with ^article\.php$ , this is from the actual request, and not from the URI that was rewritten by the previous rule. Thus, you can either add an ENV check for internal redirects, or match it with `% {THE_REQUEST}.

Your choice:

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

or

 RewriteCond %{THE_REQUEST} \ /+article\.php\?id=([0-9]{1})(\ |$) RewriteRule ^ /article/%1 [L] 
0
source

Source: https://habr.com/ru/post/957635/


All Articles