Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Chaining multiple required rewrites in .htaccess

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading