Moskau Peterson
Moskau Peterson

Reputation: 95

mod_rewrite rules not working

I want to do this with mod_rewrite .htaccess:

myurl.com/category/sub/title + optionally (/en-GB for language)
myurl.com/category/ID (number)
myurl.com/content/title (Letters with - & _)

/sub can be optionally

with:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ index.php?page=$1&sub_page=$2&tree_page=$3 [QSA]
</IfModule>

Sometimes a URL does not have the subcategory and is only the category.

However, the first code is not working. How do I achieve it for all the 3 options above?

Upvotes: 0

Views: 37

Answers (1)

arkascha
arkascha

Reputation: 42935

Instead of trying to construct a complex regex that accepts all that, go with more specialized rules:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)$ index.php?page=$1 [L, QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)$ index.php?page=$1&sub_page=$2 [L, QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ index.php?page=$1&sub_page=$2&tree_page=$3 [L, QSA]
</IfModule>

Note that I have not tested this. It is only meant to point you into the right direction...

Upvotes: 0

Related Questions