homepage Welcome to WebmasterWorld Guest from 54.243.12.156
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Htaccess regex help
Marked




msg:4332400
 4:56 am on Jun 29, 2011 (gmt 0)

Hey guys,

I have part of a URL that I would like to have changed when accessed. The part of the URL is 'forums_real' and I would like the htaccess file to redirect to the same link, except changing 'forums_real' to just 'forums'.

For example, we access the following link:
http://www.mysite.com/forums_real/index.php?user=435

And you will get redirected to:
http://www.mysite.com/forums/index.php?user=435

Thanks in advance :)

 

penders




msg:4332423
 7:14 am on Jun 29, 2011 (gmt 0)

Which part of the link is variable?

If it's simply a redirect from/to the URLs given then there is no regex required, however, I suspect you mean something like...
http://www.mysite.com/forums_real/....
Or...
http://www.mysite.com/forums_real/index.php....

?

g1smd




msg:4332424
 7:16 am on Jun 29, 2011 (gmt 0)

What code have you tried?

In order to "change" the URL you also need to change the URLs in the links on the page. The user clicks the link and their browser requests that URL.

Marked




msg:4332881
 4:35 am on Jun 30, 2011 (gmt 0)

I just realized I posted this in the wrong forum... sorry bout that.

Ah yeah, obviously I'm pretty new at this, it didn't require any regex.

I actually managed to get it myself:
RewriteEngine on
RewriteBase /
RewriteRule ^forums_real(.*?)$ forums$1 [R=301,L]

Marked




msg:4332883
 5:01 am on Jun 30, 2011 (gmt 0)

Apologies for the double post. I'm having trouble with my above code.

I would like these redirect to apply ONLY to when the user enters the URL if this is at all possible. Because this seems to be messing with both images and javascript on the site. Is that possible?

lucy24




msg:4332899
 5:48 am on Jun 30, 2011 (gmt 0)

I would like these redirect to apply ONLY to when the user enters the URL if this is at all possible.

It's sort-of approximately possible. You can make a Condition saying the Rule only kicks in when the referrer is ^-?$ (the ones that show up in your logs as "-").

Marked




msg:4332954
 9:02 am on Jun 30, 2011 (gmt 0)

Thanks for you reply.

That seems to work, aside from when there is a query string. When there's a query string then the rule does not kick in.

lucy24




msg:4333226
 7:08 pm on Jun 30, 2011 (gmt 0)

?! How do your users achieve a query string without a search string? You ought to be able to tack on a second rule specifying "and the query is null" but I can't figure out how it distinguishes between "null query" and "nonexistent query" in the %{QUERY_STRING} line.

:: looking vaguely around for g1 ::

it didn't require any regex.

I actually managed to get it myself:
RewriteEngine on
RewriteBase /
RewriteRule ^forums_real(.*?)$ forums$1

Er, that is a regex ;) It's just a very minimalist one. But if you simply replace "forums_real" with "forums", everything else should come along for the ride anyway.

Gotta say the "messing with images and javascript" makes me a bit uneasy because there shouldn't be any effect. Unless you've got other code pertaining to images and javascript that itself looks at the referrer.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved