Michelle
Michelle

Reputation: 2235

Redirect .php to extensionless URL only for non-dynamic URL's

I'm trying to 301 redirect pages with a .php extension to their extensionless version. Using the rule below, pages with a query parameter fail (signup.php?login=true) or result in a redirect loop.

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ /$1 [R=301]

I'd therefor like to exclude dynamic urls (It doesn't seem to make sense to rewrite them anyway). I've written the code below but the condition still seems to match dynamic urls.

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ /$1 [R=301]

Can anyone help me adjust my condition to prevent dynamic urls from being rewritten.

Upvotes: 0

Views: 461

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9539

Try the following. Ensure that there are 2 spaces between \.php\ and `[NC]

RewriteEngine on
RewriteBase /

#any url with .php and no anchor or query string
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^\ ]+)\.php\  [NC]
RewriteRule ^ /%1 [R=301]

Upvotes: 1

Related Questions