Viktor Svensson
Viktor Svensson

Reputation: 755

.htaccess - 3 different rewrites for 3 different URL structures

I'm trying to achieve the following using htaccess on my blog portal:

STRUCTURE 1

example.domain.com/link/sublink -> /blogdisplay.php?page=example&page2=link&page3=sublink

STRUCTURE 2

en.domain.com/link/sublink/subsublink -> /index.php?lang=en&page=link&page2=sublink&page3=subsublink

STRUCTURE 3

domain.com/link/sublink/subsublink -> /index.php?page=link&page2=sublink&page3=subsublink

How do I achieve this? This is my current code (which doesn't handle languages at all right now, but the two other URL structures work fine):

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]{3,})\.domain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1&page3=$2 [L,QSA]

## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]

Upvotes: 1

Views: 214

Answers (1)

anubhava
anubhava

Reputation: 786091

For language case you can add this additional case (just below R=301 rule):

RewriteCond %{HTTP_HOST} ^([a-z]{2})\.domain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?lang=%1&page=$1&page2=$2&page3=$3 [L,NC,QSA]

PS: Also I suggest moving If the request is for a valid file rule just below above language rule.

Upvotes: 2

Related Questions