Welcome to WebmasterWorld Guest from 34.235.143.190

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

htaccess multiple 301 redirects

     
5:51 am on Mar 28, 2014 (gmt 0)

New User

5+ Year Member

joined:Mar 28, 2014
posts:2
votes: 0


Hello..

I am redesign my website and changes some category, sub category and product URL, but now I want to redirect old URL into new one, I have more than 50 URL redirect to new URL, Currently I was using below method

Redirect 301 http://www.example.com/stone-and-tile/ceramic-and-porcelain-tile/ambienti/index.php http://example.com/new/tile-and-stone-floor/ceramic-and-porcelain-tile/supergres-ambienti/index.php

Redirect 301 http://www.example.com/stone-and-tile/ceramic-and-porcelain-tile/ardesign/index.php http://example.com/new/tile-and-stone-floor/ceramic-and-porcelain-tile/panaria-ardesign/index.phpPlease

As per describe above I have redirect more than 50 URLs, so please suggest me any short code or any other way I can reduce this code and also my website not getting slow down

[edited by: phranque at 4:02 pm (utc) on Mar 28, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

8:28 am on Mar 28, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15638
votes: 795


Yikes. It's just as well the Forums software obliterated the example links, because I seriously doubt you meant for them to say
blahblahblahindex.phphttp://blahblahblah
in the middle of each one.

What's the pattern? Is there some element in Old URL that will be recycled in New URL?

At an absolute minimum, you'll have to change from Redirect to RedirectMatch so you can use regular expressions with capturing and reusing. But before we get there you'll need to explain in English what you're aiming for. First figure out what exactly you need a rule to do. Then start writing the rule.
9:09 am on Mar 28, 2014 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 9, 2014
posts: 150
votes: 0


Your .htaccess will probably look something like this:

RewriteEngine on

# change
# /stone-and-tile/ceramic-and-porcelain-tile/ambienti/index.php
# to
# /index.php?cat=stone-and-tile&subcat=ceramic-and-porcelain-tile&everythingelse=ambienti
RewriteRule ^(.*?)/(.*?)/(.*?) index.php?cat=$1&subcat=$2&everythingelse=$3 [QSA,L]

Like Lucy said, though, if you can give us a few examples (leave off the domain name so that the forum won't format it) then we might be able to suggest reusable patterns to make it easier.
12:57 pm on Mar 31, 2014 (gmt 0)

New User

5+ Year Member

joined:Mar 28, 2014
posts:2
votes: 0


thank you for give me nice feedback
4:03 pm on Mar 31, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15638
votes: 795


Follow-up: The rules as written will always fail, in any module. afaik nothing in Apache looks at protocol-and-domain in the pattern of a rule. Only in the target.
7:46 pm on Mar 31, 2014 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 9, 2014
posts: 150
votes: 0


Technically speaking, Lucy, you could refer to the domain using %{HTTP_HOST}. But that's not relevant to the OP, just a commentary on your last post :-)

For the OP, I think you're looking for something like:

RewriteRule stone-and-tile/ceramic-and-porcelain-tile/ardesign/index.php http://example.com/new/tile-and-stone-floor/ceramic-and-porcelain-tile/panaria-ardesign/index.php [R=301, QSA, L]

(That should all be on one line)

This would be a visible redirect; meaning that the address bar would visibly show the new address. If you wanted it to be invisible (they still see the old address, but get the new information), you would use:

RewriteRule stone-and-tile/ceramic-and-porcelain-tile/ardesign/index.php new/tile-and-stone-floor/ceramic-and-porcelain-tile/panaria-ardesign/index.php [QSA, L]

If you have several items listed under "stone-and-tile", though, then you may be able to make it a little more dynamic with something like:

# convert all with "stone-and-tile" in the top directory to "new/tile-and-stone-floor"
RewriteCond %{REQUEST_URI} ^/(stone-and-tile)/ [NC]
RewriteRule ^%1/(.*?)$ new/tile-and-stone-floor/$1 [R=301,QSA,L]
12:43 am on Apr 1, 2014 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:11681
votes: 205


welcome to WebmasterWorld, amit magento!


a couple of suggestion and corrections for GoNC's post:
i would suggest redirecting to the directory root and make sure index.php is specified as the default directory index document.
the QSA flag is unnecessary if you aren't redirecting to a url with a query string.
your Substitution string for external redirects should always begin with the canonical protocol and hostname.
6:48 am on Apr 1, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15638
votes: 795


you could refer to the domain using %{HTTP_HOST}

Sure, but that's a condition. Similarly the at-least-three different ways of establishing http vs. https.

I meant the body of a rule. And, of course, mod_alias doesn't have conditions, so no joy there.
7:56 am on Apr 1, 2014 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 9, 2014
posts: 150
votes: 0


your Substitution string for external redirects should always begin with the canonical protocol and hostname.


Why's that, phranque? If he's rewriting to the same domain and protocol with a 301 redirect, why is it preferred? Does it make any difference in overhead?

I ask because I use this a lot on my sites, and if I'm adding to the server load then it might be worth a change.

I completely agree with the other two notes, though. I included QSA, though, because I don't think it adds any overhead, and I figured it's better to use it in case the OP had query strings that he didn't mention.
8:04 am on Apr 1, 2014 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 9, 2014
posts: 150
votes: 0


BTW, to the OP, when we refer to QSA, this means "query string append". It means that if your original link has a query string, like:

index.php?variable=something

then the redirect will keep the ?variable=something at the end of the new link.

If you don't have a query string on any of the links, then you can remove the QSA from the end of the rule, where I had [R=301, QSA, L] or [QSA, L]
1:41 pm on Apr 1, 2014 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:11681
votes: 205


your Substitution string for external redirects should always begin with the canonical protocol and hostname.

Why's that, phranque? If he's rewriting to the same domain and protocol with a 301 redirect, why is it preferred?


suppose these are your mod_rewrite directives:
# externally redirect some old path to a new path
RewriteRule old/path /new/path [R=301, L]

# after all the specific redirects above, do a general hostname canonicalization redirect
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


suppose the request is for http://www.example.com/old/path - the server will return a 301 status code and a Location: /new/path header, so the browser requests http://www.example.com/new/path and the server will return another 301 status code and a Location: http://example.com/new/path header.

if you provide the canonical protocol and hostname you avoid the multiple redirect hops.



when we refer to QSA, this means "query string append". It means that if your original link has a query string, like:

index.php?variable=something

then the redirect will keep the ?variable=something at the end of the new link.

in addition...

RewriteRule Flags - QSA:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule /pages/(.+) /page.php?page=$1 [QSA]


With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.
10:19 pm on Apr 1, 2014 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


If you are redirecting requests for an old URL such as
http://www.example.com/?pageid=45&pagename=some-page
or
http://www.example.com/index.php?pageid=45&pagename=some-page

to a new friendly URL such as
http://www.example.com/p45-some-page

you would normally use a rule such as
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?pageid=([0-9]+)&pagename=([a-z0-9-]+)\ HTTP/
RewriteRule ^(index\.php)?$ http://www.example.com/p%1-%2? [R=301,L]

with a question mark on the end of the rule target to suppress query strings and omitting the [QSA] flag, otherwise the original request will be redirected to
http://www.example.com/p45-some-page?pageid=45&pagename=some-page

instead of the required URL, which is
http://www.example.com/p45-some-page


If have a friendly page request such as
http://www.example.com/p45-some-page?sort_order=reverse

the internal rewrite will look something like this
RewriteRule ^p([0-9]+)-(.*) /index.php?pageid=$1&pagename=$2 [L,QSA]

with the QSA flag included so that the
sort_order
parameter is also passed through to the
index.php
script.
11:44 pm on Apr 1, 2014 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15638
votes: 795


If he's rewriting to the same domain and protocol

That's exactly the point. Omit protocol-plus-domain from the target, and the redirect will use whatever was in the original request-- even it if's wrong (with/without www, http vs. https on sites that have both).
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members