D.Sanchez
D.Sanchez

Reputation: 49

.htaccess for directory and subdirectory levels

I need to redirect to diferents files when the URL have 1 directory or 2 subdirectories.

http://example.com/content/category-1/
http://example.com/content/category-2/
http://example.com/content/category-3/
This will execute "category.php"

http://example.com/content/category-1/subcategory-5/
http://example.com/content/category-2/subcategory-21/
http://example.com/content/category-3/subcategory-88/
This will execute "subcategory.php"

Using next code on .htaccess the first option is working, but I need the other Condition to work the second.

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_URI} /content/(.*)(/*)
RewriteRule (.*) /category.php?id=$1

Thanks!

Upvotes: 1

Views: 448

Answers (1)

nice_dev
nice_dev

Reputation: 17805

You are almost there. You will have to replicate the last rewrite rule again further matching the next subcategory values if available.

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteRule ^/?content/([^/]+)/([^/]+)/?$ /subcategory.php?id=$1&subcat=$2 [L,NC,NE,QSA]

RewriteRule ^/?content/([^/]+)/?$ /category.php?id=$1 [L,NC,QSA]

Upvotes: 2

Related Questions