PimPee
PimPee

Reputation: 47

RewriteRule same name as directory

For my site I have a directory called /test/. I want to rewrite www.example.com/nl/test and www.example.com/nl/test/ to a certain page (test.php).

Some global conditions (for all the rules)

RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R]
RewriteBase /

RewriteRule ^(nl|en)$ $1/ [NC,R]

RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]

RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]

RewriteRule ^sale$ sale.php
RewriteRule ^valentine$ valentine.php

Some conditions for the rewrite + folder

RewriteRule ^test/$ test.php

The redirect of www.example.com/nl/test/ is correct. The language parameter is also correctly rewritten.

For the second redirect (the version without the trailing slash) I can't get this working.

RewriteRule ^test$ test.php

Now my URL is rewritten as www.example.com/test/?language=nl

Can someone give me a tip or hint to fix this? I can't change the name of the directory since there are several external URLs linking to this directory.

Upvotes: 0

Views: 1162

Answers (1)

LazyOne
LazyOne

Reputation: 165148

This rule will do the whole job (instead of 4 lines you have there): it will rewrite both /nl/test and /nl/test/ to /test.php?language=nl.

RewriteRule ^(en|nl)/test/?$ /test.php?language=$1 [NC,QSA,L]

NOTES:

The [QSA] flag will preserve any existing query string (therefore, there is no need for &%{QUERY_STRING}).


Full .htaccess:

Options +FollowSymLinks -MultiViews
DirectorySlash Off

RewriteEngine On
RewriteBase /

RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]

RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ /en/$2 [R=302,L]

RewriteRule ^(nl|en)/(.*)$ /$2?language=$1 [NC,QSA,L]

RewriteRule ^sale/?$ sale.php [QSA,L]
RewriteRule ^valentine/?$ valentine.php [QSA,L]
RewriteRule ^test/?$ test.php [QSA,L]

NOTES:

There is no need for RewriteRule ^(nl|en)$ $1/ [NC,R] as you already have RewriteRule ^(nl|en)$ http://www.example.com/$1/ [NC,R=301,L]. It does the same job.

Upvotes: 2

Related Questions