Forum Moderators: phranque

Message Too Old, No Replies

Redirecting www.domain.com/path1/. to www.domain.com/path2/.

         

grintoul

9:38 am on Mar 2, 2007 (gmt 0)

10+ Year Member



Hi everyone,

Was wondering if someone could help me... I don't really understand mod_rewrites and the like (I tend to just gain what I can from forums and cobble it together!) and can't find a solution to this problem.

Whenever a URL starts with:

www.domain.com/path1/rest-of-the-path...

I want it to redirect to:

www.domain.com/path2/rest-of-the-path...

Some examples:

www.domain.com/path1/ TO www.domain.com/path2/
www.domain.com/path1/tags/ TO www.domain.com/path2/tags/
www.domain.com/path1/tags/x/y/z TO www.domain.com/path2/tags/x/y/z
www.domain.com/otherpath UNAFFECTED

Can anyone help me?

Thanks in advance and sorry for being such a newbie,

Guy

grintoul

11:58 am on Mar 2, 2007 (gmt 0)

10+ Year Member



Hi

Just thought, maybe better explained using pseudo code. I need a rule that does this:

If domain starts with [domain.com...] ...
Then change it to [domain.com...] ...

I know it's no doubt simple, but don't have a clue how to do it :-(

Regards,
Guy

jdMorgan

2:24 pm on Mar 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> If domain starts with http://www.domain.com/path1/

That is not a domain, it is a domain followed by a URL-path.

This is a basic redirect/rewrite question, already well-covered in the mod_alias [httpd.apache.org] and mod_rewrite [httpd.apache.org] documentation.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

grintoul

5:10 pm on Mar 5, 2007 (gmt 0)

10+ Year Member



Hi,

I looked at the documentation but I don't understand it. My best attempt is:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^http://www.example.com/tag/ [NC]
RewriteRule ^(.*)$ http://www.example.com/category/$1/ [L,R=301]

So if the URL starts with http://www.example.com/tag/, change it to read http://www.example.com/category/

This should mean that, for instance, http://www.example.com/tag/apache becomes http://www.example.com/category/apache but I think I've done something wrong?

Can anyone help?

Thanks,
Guy

jdMorgan

5:29 pm on Mar 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are mixing URL-parts and trying to match them against variables that do not contain those parts. For example, REQUEST_URI never contains the www.example.com part, only what follows that. Please see the links provided above; There is no short-cut, and using code samples from forums without verifying them against the docs is not at all healthy for your search engine rankings -- There is a lot more bad/buggy/unoptimized code on the WWW than there is good code. How can you tell good from bad without checking it yourself?

www.domain.com/path1/ TO www.domain.com/path2/
www.domain.com/path1/tags/ TO www.domain.com/path2/tags/
www.domain.com/path1/tags/x/y/z TO www.domain.com/path2/tags/x/y/z
www.domain.com/otherpath UNAFFECTED

 RewriteEngine on
#
RewriteRule ^path1/(.*)$ http://www.example.com/path2/$1 [R=301,L]

To clarify the statement above, if a request is received for the URL example.com/path1/page?abc=def
%{HTTP_HOST} would contain "example.com"
%{REQUEST_URI} would contain "/path1/page"
%{QUERY_STRING} would contain "abc=def"
and the RewriteRule pattern would be matched against "path1/page" in .htaccess -- Note that there is no leading slash in the .htaccess context, as compared to the %{REQUEST_URI} value shown above.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

grintoul

5:45 pm on Mar 5, 2007 (gmt 0)

10+ Year Member



Thanks for your help :-)

Sorry, I didn't meant to be a pain and I looked at the documentation, I'm just new at this so I found it all a bit overwhelming

jdMorgan

8:22 pm on Mar 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No pain at all -- Those were earnest warnings meant to keep you from getting into trouble... :)

(And, taking the warning above about copying code from forums, you should check *my* code against the documentation, too! I wouldn't be offended in the least.) ;)

Jim