Ad

.htaccess Rewrite Url For Seo Friendly Urls Causes 500 Error

- 1 answer

Here are some example urls I wish to rewrite:

https://website.com?c=food&s=cheese&p=3
https://website.com?c=food&s=cheese
https://website.com?s=cheese

I wish for the above to be rewritten to:

https://website.com/food/cheese/3
https://website.com/food/cheese
https://website.com/cheese

Here is my htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /?c=$1&s=$2&p=$3 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?c=$1&s=$2 [L]
RewriteRule ^([^/]*)$ /?s=$1 [L]

Can anyone see whats going wrong? I a remove the last line

RewriteRule ^([^/]*)$ /?s=$1 [L]

The page loads however, it seems to be rewriting the css and js files?

Ad

Answer

A RewriteCond is only applicable to the immediate next RewriteRule. Since you have multiple rules needing same conditions, it is better to define a separate rule to exclude all files, directories and known URIs.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ ?c=$1&s=$2&p=$3 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/?$ ?c=$1&s=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ ?s=$1 [L,QSA]
Ad
source: stackoverflow.com
Ad