Welcome to WebmasterWorld Guest from 54.226.62.251

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite/regex question

     

westman7

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

5+ Year Member



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!

jdMorgan

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

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



Change the regex to:

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

Jim

westman7

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

5+ Year Member



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]

jdMorgan

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

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



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month