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

Apache Web Server Forum

    
Redirect rule
sam222




msg:4473603
 6:03 am on Jul 8, 2012 (gmt 0)

RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L]

Redirects works fine if i use http://example.com which redirects it to http://www.example.com but when i use some inner page as
http://example.com/results/?type1=&type2=&type3=&price=&x=31&y=7&flag=

then i want it to be redirected to http://www.example.com/results/?type1=&type2=&type3=&price=&x=31&y=7&flag=

For any http:// URL it should be redirected to http://www to avoid URLs duplication.

[edited by: incrediBILL at 7:44 am (utc) on Jul 8, 2012]
[edit reason] fixed formatting [/edit]

 

lucy24




msg:4473613
 7:36 am on Jul 8, 2012 (gmt 0)

Uhm... Your question reads as if it's a continuation from an ongoing thread. I don't think it really is. But I think you've become so absorbed in the problem that you forget we're hearing about it for the first time.

In English: What are you trying to do? At a guess, you want to redirect everyone to the canonical version of your site name-- with or without www-- but I'm not betting money on it.

Before you answer, read the post at the top of this Forum about using example.com. Matter of fact, read some random other posts and you may never need to come back at all. This is an amazingly repetive Forum. Maybe something about Apache makes people afraid to look at other people's questions even if they seem to be identical.

In mod_rewrite you do not need to escape / slashes. As far as I know, you never need to escape colons anywhere. You do need to escape literal periods. BUT you do not need to escape anything at all, whatsoever, of any kind, in the target (the part on the right).

Now, about that mile-long query string that you haven't even mentioned... ;)

sam222




msg:4473616
 8:01 am on Jul 8, 2012 (gmt 0)

Thanks for the reply.

In English: What are you trying to do? At a guess, you want to redirect everyone to the canonical version of your site name-- with or without www-- but I'm not betting money on it.


I want all of my non www urls to be redirected to www urls to avoid duplication.

g1smd




msg:4473620
 9:41 am on Jul 8, 2012 (gmt 0)

^/?$ matches requests for example.com/ and for example.com// only, so no inner pages will ever be affected by this rule.

You'll need (.*) to match all pages.

You'll also need a RewriteCond testing that the requested hostname was not "exactly" www.example.com

Escaping should not be used in the rule target, nor should quotes.

Check recent threads for example code. This is a question covered several times each month.

lucy24




msg:4473629
 10:52 am on Jul 8, 2012 (gmt 0)

Redirects works fine if i use http://example.com which redirects it to http://www.example.com

Ah, but what if you had said www.example.com in the first place? You would still be redirected, and the request would go around in circles until the user's browser (not your server) put a stop to it.

General rule: When testing something, don't only test it on things that are supposed to work. Also test it on things that are not supposed to work-- or that are supposed to not work, depending on how you look at it. And test it on garbage to make sure it's GIGO. (This is what is supposed to happen.)

Here you need a RewriteCond to ensure that only the right things-- er, that is, the wrong things-- get redirected. Exact wording will be found in many many threads in this forum. Conceptually what you need to say is: "If the requested host is NOT exactly such-and-such, then redirect to such-and-such".

As Tolstoy said: correct URLs are all alike. Incorrect URLs are each incorrect in their own way.

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