Welcome to WebmasterWorld Guest from 54.205.126.164

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite/regex question

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

New User

5+ Year Member

joined:Feb 16, 2009
posts: 31
votes: 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:

[somedomain.com...]

...into...

[somedomain.com...]

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.

HOWEVER!

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)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Change the regex to:

^([^/]+(/[^/]+)?)$

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

New User

5+ Year Member

joined:Feb 16, 2009
posts:31
votes: 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)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 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.

Jim

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members