homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

mod rewrite/regex question

 11:42 pm on Jul 31, 2009 (gmt 0)

OK. So I'm trying to figure out how to transform my old mod_rewrite rule (which I've got thanks to this forum!) into this new one I need...

Here's the old one:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/somedir/$1/ !-d
RewriteRule ^([^/]+)/$ [somedomain.com...] [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /somedir/script.cgi?cat=$1 [L]

What I want to create right now is a new htaccess file for a new dir that will help to transform the following:




So as you can see I want to pass the "Some-String-1/Some-String-2" part directly to the script as a variable. I don't want to pass "Some-String-1" as one var and "Some-String-2" as another, but instead both of them WITH the "/" as one.


It is possible that inside the link there will be only "Some-String-1" eg. [somedomain.com...] then I'd want to pass just "Some-String-1" to the script.

Please tell me it can be done - beer's on me!

BTW: the dashes in "Some-String-1/Some-String-2" - are there intentionally as I they will be present in the original version as well.

Thanks a lot!



 12:44 am on Aug 1, 2009 (gmt 0)

Change the regex to:




 1:49 am on Aug 1, 2009 (gmt 0)

Tried it but here's what I get...

If I go to somedomain.com/dir/String-1 FF gives some error saying "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." I guess some redirect loop might be occurring?

If I type in somedomain.com/dir/String-1/ WITH a slash it simply gives a 404 "The requested URL /dir/String-1/ was not found on this server."

Now, as far as I can see the regex looks perfectly valid...

Here's exactly what I use in the htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/dir/$1/ !-d
RewriteRule ^([^/]+(/[^/]+)?)$ [somedomain.com...] [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+(/[^/]+)?)$ /dir/script.cgi?var=$1 [L]


 5:55 pm on Aug 1, 2009 (gmt 0)

Your first rule will loop, adding "/dir" repeatedly unless you prevent it:

[i]RewriteCond %{REQUEST_URI} !^/dir/[/i]
RewriteCond %{DOCUMENT_ROOT}/dir/$1/ !-d
RewriteRule ^([^/]+(/[^/]+)?)$ http://www.example.com/dir/$1 [R=301,L]

Whenever you get any error, a quick look at your server error log file is often quite informative.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved