Home > OS >  Chaining multiple required rewrites in .htaccess
Chaining multiple required rewrites in .htaccess

Time:01-10

I have at present 2 rewrites that are supposed to occur on any given url,

The first redirects any url matching the pattern %DOMAIN%/mcc-* to show.php?id=mcc-* The second redirects any url without the .php extension to the page as if it had one.

The first rewrite gets called and works, but the second does not.

I have tried swapping the places of the rewrites, but the result is the same, the 'Add Extension' rewrite is never called, even on pages that don't redirect to show.php

ErrorDocument 404 /error/404.php

Options All -Indexes -MultiViews

RewriteEngine On

# Silent Redirect from any url ending with mcc-* to show.php?id=mcc-*
# This works as intended.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(mcc-[\d] )/?$ show.php?id=$1 [QSA,NC]

# Allow urls to not include the .php extension
# This is not not being called.
RewriteBase /
RewriteCond %{REQUEST_URI}/$1.php -f [NC]
RewriteRule ^(. ?)/?$ $1.php [L]

Is there someway to get both of these conditionals to run, or at the very least run the second if the first doesn't match?

CodePudding user response:

With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

ErrorDocument 404 /error/404.php
Options All -Indexes -MultiViews

RewriteEngine ON

# Allow urls to not include the .php extension
# This is not not being called.
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(. ?)/?$ $1.php [L]

# Silent Redirect from any url ending with mcc-* to show.php?id=mcc-*
# This works as intended.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(mcc-[\d] )/?$ show.php?id=$1 [QSA,NC]
  • Related