Dodo
Dodo

Reputation: 69

htaccess rewriting URLs with multiple forward slashes

Trying to work out a set of .htaccess mod_rewrite rules that will set these links:

domain.com/content-page
domain.com/region/another-page
domain.com/region/brand/more-content-here

to fetch files:

domain.com/content-page.php
domain.com/region-another-page.php
domain.com/region-brand-more-content-here.php

Where 'region' and 'brand' and the page name are all variable/changeable.

So far I have:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,C]
RewriteRule ([^/]+)/([^/]+) $1-$2 [NC,L] 

This works but only for one forward slash. As mentioned above, URLs after the domain may have none, one or two forward slashes. Haven't had any luck trying to extend this to deal with multiple (or not) slashes.

Upvotes: 2

Views: 703

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Try:

RewriteEngine On

# pass through root
RewriteRUle ^(index\.php)?$ - [L]

# no more / so add extension
RewriteCond $1 !/
RewriteCond $1 !\.php$
RewriteCond ${REQUEST_FILEAME} !-f
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)/(.*)$ /$1-$2 [L]

Upvotes: 3

Related Questions