A bit stuck on a simple rewrite

New to mod_rewrite? This is a good place to start.

A bit stuck on a simple rewrite

Postby chidge » Sun Aug 16, 2009 5:54 am

hi guys I think im just tired but I am stuck on a simple rewrite and cant work it out

simple I want to redirect anything that comes in on my home url that is a word or _


Code: Select all
\w


and between 8 and 16 characters to a sub folder, but there are certain names that fit this description that i dont wish to redirect


so i want www.example.com/great_stuff

to go to a folder that exists in my web root called /great_stuff (as would normally happen)



and www.example.com/otherthingy

which is a folder that doesnt exist in my web root but matches the rewrite conditions of
Code: Select all
[\w]{8,16}
and then redirects to

/subfolder/otherthingy but in the url shows www.example.com/otherthingy




so to redirect i have


Code: Select all
RewriteRule ^/([\w]{8,16})/?$ /subfolder/$1/index.php [L]


but I cant get my head around how to say not redirect for the value

"brilliant" "excellent" "another_one" or any others


Code: Select all
RewriteRule ^/((^brilliant|excellent|another_one)[\w]{8,16})/?$ /subfolder/$1/index.php [L]



Code: Select all
RewriteRule ^/(!(brilliant|excellent|another_one)[\w]{8,16})/?$ /subfolder/$1/index.php [L]


these arnt working and my logs are showing its trying to look in the subfolder redirect please can you point me in the right direction I have gone all fuzzy on this (maybe i shouldnt be trying this on a sunday)

I also have a feeling this has something to do with a rewrite condition

Code: Select all
RewriteCond %{REQUEST_FILENAME} !-d


but if i create
Code: Select all
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([\w]{8,16})/?$ /subfolder/$1/index.php [L]


as i understand this says if the directory doesnt exist then perform this rule - otherwise skip it?

Bit frustrated on this and could do with some light being shed on it -Thanks in advance for any pointers
chidge
 
Posts: 3
Joined: Sun Aug 16, 2009 5:44 am

Postby Timbo » Sun Aug 16, 2009 6:36 am

RewriteCond %{QUERY_STRING} !^(brilliant|excellent|another_one)$
RewriteRule ^/([a-z_]{1,}[\w]{8,16})/?$ /subfolder/$1/index.php [L]


RewriteCond %{REQUEST_FILENAME} !-d => only rewrite if folder does not exist

RewriteCond %{REQUEST_FILENAME} !-f => only rewrite if file does not exist
Timbo
 
Posts: 7
Joined: Sun Aug 16, 2009 1:05 am

Postby richardk » Sun Aug 16, 2009 7:34 am

Code: Select all
RewriteCond %{QUERY_STRING} !^(brilliant|excellent|another_one)$
RewriteRule ^/([a-z_]{1,}[\w]{8,16})/?$ /subfolder/$1/index.php [L]

You want REQUEST_URI not QUERY_STRING.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(brilliant|excellent|another_one)(/.*)?$
RewriteRule ^(\w{8,16})(/(.*))?$ /subfolder/$1/$3 [QSA,L]


Or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(\w{8,16})(/(.*))?$ /subfolder/$1/$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby chidge » Sun Aug 16, 2009 7:53 am

thanks richard I had to add a forward slash at the start of the rewrite and that worked perfect, you have also added for anything extra on the end of the urls

many thanks.


Code: Select all

   RewriteCond %{REQUEST_URI} !^/(brilliant|excellent|another_one)(/.*)?$
   RewriteRule ^/(\w{8,16})(/(.*))?$ /subfolder/$1/$3 [QSA,L]
chidge
 
Posts: 3
Joined: Sun Aug 16, 2009 5:44 am

Postby richardk » Sun Aug 16, 2009 5:01 pm

Are you putting the mod_rewrite in the httpd.conf file (or similar) or a <VirtualHost>? If you are you would need
Code: Select all
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d

not
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-d
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby chidge » Sun Aug 16, 2009 10:58 pm

this is going in htaccess due to not being able to get at the <virtual host>

thanks
chidge
 
Posts: 3
Joined: Sun Aug 16, 2009 5:44 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 27 guests

cron