Welcome to WebmasterWorld Guest from 54.161.149.107

Forum Moderators: coopster & jatar k

Htaccess regex help

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

5+ Year Member



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 :)
7:14 am on Jun 29, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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....


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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.
4:35 am on Jun 30, 2011 (gmt 0)

5+ Year Member



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]
5:01 am on Jun 30, 2011 (gmt 0)

5+ Year Member



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?
5:48 am on Jun 30, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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 "-").
9:02 am on Jun 30, 2011 (gmt 0)

5+ Year Member



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.
7:08 pm on Jun 30, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



?! 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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month