llanato
llanato

Reputation: 2491

Odd Mod_Rewrite Behaviour

I've had a look at a few mod_rewrite questions here but nothing they have suggested has worked to date.

I have the below rules:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]*)/$ ?action=$1 [L]

## General Pages
RewriteRule ^privacy-policy/$ ?action=privacy-policy [L]
RewriteRule ^terms-conditions/$ ?action=terms-conditions [L]
RewriteRule ^about/$ ?action=about [L]
RewriteRule ^contact-us/$ ?action=contact-us [L]
RewriteRule ^sitemap.xml$ ?action=sitemap [L]

## Blog Pages
RewriteRule ^blog/$ ?action=blog [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ ?action=blog&category=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ ?action=blog&category=$1&id=$2&title=$3 [L]

The first blog rule works fine and the third blog rule works fine.

The second blog rule isn't working correctly, lets say I have the below URL:

/blog/general/

The second blog rule is returning blog/general as the value for categorywhich is incorrect, it should only be returning general as the value for category.

This issue only occurs on my production environment, it doesn't happen on my local dev environment.

I'm really stumped on this one, any suggestions greatly appreciated.

Upvotes: 1

Views: 22

Answers (1)

anubhava
anubhava

Reputation: 785146

Try reordering your rules and merge SSL with www rule to avoid multiple redirects (bad for SEO and client experience):

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

## General Pages
RewriteRule ^privacy-policy/$ ?action=privacy-policy [L]
RewriteRule ^terms-conditions/$ ?action=terms-conditions [L]
RewriteRule ^about/$ ?action=about [L]
RewriteRule ^contact-us/$ ?action=contact-us [L]
RewriteRule ^sitemap.xml$ ?action=sitemap [L]

## Blog Pages
RewriteRule ^blog/$ ?action=blog [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ ?action=blog&category=$1 [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ ?action=blog&category=$1&id=$2&title=$3 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]*)/$ ?action=$1 [L,QSA]

Upvotes: 1

Related Questions