KJS KJS - 3 months ago 20
PHP Question

RewriteRule for SEO url's

I'd like to use RewriteRule but can't seem to get the hang of it.

These are the layers of the website:

http://domain.com/artists/artist-name/1/2/3/
http://domain.com/fairytales/fairy-tale-one/1/2/3/
http://domain.com/horrorstories/horror-story-six/1/2/3/


Basically, it is the dir before the '/1/' in the url's that should be the cool SEO url name.

I understood so far, it should be something like this to rewrite propely.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^/artists/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?artist_id=$1&cat_id=$2&style_id=$3
RewriteRule ^/fairytales/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?story_id=$1&cat_id=$2&style_id=$3
RewriteRule ^/horrorstories/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?story_id=$1&cat_id=$2&style_id=$3


.htaccess works with Apache 2.4.23 on the CentOS server i am using.

After the many 500 errors and now too many 404's, I'm finally asking.

Answer

Your regex patterns are not matching given URLs. Try these rules:

RewriteEngine On

RewriteRule ^/?artists/[\w-]+/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?artist_id=$1&cat_id=$2&style_id=$3 [L,QSA,NC]

RewriteRule ^/?fairytales/[\w-]+/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?story_id=$1&cat_id=$2&style_id=$3 [L,QSA,NC]

RewriteRule ^/?horrorstories/[\w-]+/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?story_id=$1&cat_id=$2&style_id=$3 [L,QSA,NC]
Comments