Forum Moderators: phranque

Message Too Old, No Replies

Redirect directive with slashes

         

kjh23

4:53 pm on Jul 9, 2012 (gmt 0)

10+ Year Member



Hi,

I have a big list (approx 500) of request URLs and target URLs. I want to 301 redirect the request URLs to the target URLs exactly as is. Therefore I don't need any regex or anything complicated. I was looking at using just the redirect directive, but at the request URIs have slashes in them (eg /dir1/dir2/mypage.html), it doesn't work.

What's the correct thing to use in this situation?

g1smd

8:29 pm on Jul 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use a RewriteRule with the [R=301,L] flags.

Use the RightRegex pattern to match the requests. Capture the right bits in a backreference to use in $1 on the rule target.

Specify the protocol and domain at the beginning of the rule target.

kjh23

8:53 pm on Jul 9, 2012 (gmt 0)

10+ Year Member



I don't want to write it in regex because there are hundreds and there is no specific pattern. Each source link and been checked manually and a specific target link picked, so I don't need to capture anything or match against any patterns. I just want to set up a list of from and to redirects.

I just don't know what the right syntax is in apache to do this.

g1smd

9:00 pm on Jul 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Without seeing the requested and target path examples, it is impossible to say.

If the old and new paths are identical, just on different domains, the RegEx pattern is (.*) and one rule does for the whole site.

If the old and new paths are completely different, you'll need one rule for each URL. The pattern will be the requested path.

Use example.com in this forum.

lucy24

11:17 pm on Jul 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was looking at using just the redirect directive, but at the request URIs have slashes in them (eg /dir1/dir2/mypage.html), it doesn't work.

That wouldn't prevent a Redirect (by that name) from working. You just need to know that mod_rewrite and mod_alias behave differently, although they can both issue the identical 301. In what follows, don't quote me on the leading and trailing slashes, I'm just illustrating the point.

Redirect 301 /dir1/dir2/ http://www.example.com/dir3/
vs.
RewriteRule /dir1/dir2/ http://www.example.com/dir3/ [R=301,L]

If you request
http://www.example.com/dir1/dir2/dir4/foobar.html

the first Rule (using mod_alias) will send you to
http://www.example.com/dir3/dir4/foobar.html
because it reappends the part of the path that isn't spelled out in the Rule.

The second Rule (using mod_rewrite) will send you to
http://www.example.com/dir3/
(that is, /dir3/index.html or equivalent)
because anything that isn't spelled out in the target is simply thrown away.

To redirect to the specific page
http://www.example.com/dir3/dir4/foobar.html

using mod_rewrite, you would need to say either
RewriteRule ^dir1/dir2/dir4/foobar\.html http://www.example.com/dir3/dir4/foobar.html [R=301,L]

or
RewriteRule ^dir1/dir2/(dir4/foobar\.html) http://www.example.com/dir3/$1 [R=301,L]

But wait! If you've got two pages living side by side in the same directory, and their new addresses will also be side by side:

RedirectMatch 301 /dir1/dir2/dir4/(foobar|zoetrope)\.html http://www.example.com/dir3/dir4/$1.html
or even
RedirectMatch 301 /dir1/dir2(/dir4/(foobar|zoetrope)\.html) http://www.example.com/dir3$1

or

RewriteRule /dir1/dir2/dir4/(foobar|zoetrope)\.html http://www.example.com/dir3/dir4/$1.html [R=301,L]
or even
RewriteRule /dir1/dir2(/dir4/(foobar|zoetrope)\.html) http://www.example.com/dir3$1 [R=301,L]


Congratulations. You've just constructed a Regular Expression. Wasn't that painless?

If none of your Redirects have any conditions, and you don't have any existing RewriteRules, you can use mod_alias (Redirect by that name). In fact, Apache wants you to. But if you have-- or ever expect to have-- even a single RewriteRule, convert all your Redirects to RewriteRules with the [R=301] flag.

kjh23

10:27 am on Jul 10, 2012 (gmt 0)

10+ Year Member



Thank you for the help. But I don't think I've explained myself very well at all. The situation is that a new site will go live on the same domain. All the old pages are gone but someone has made a spreadsheet with all the old pages in column A and the most appropriate page to send them to in column B. There is no common pattern, hence why it was done manually. I don't want to manually write the regex out for each one. I want a single piece of apache syntax that will take the request, match it exactly and redirect to the target without capturing or adding anything to it. That way, I can write a formula to create the redirects in column c, and just copy them out to the apache conf or a htaccess.

I was hoping to use the redirect directive because then I don't have to worry about escaping special regex characters. What I currently get is for example:

Redirect 301 /about http://www.example.com/info/company_info.html
Redirect 301 /about/clients.html http://www.example.com/client_list.html

The first redirect will work, but the second one will match on the first one and send to /info/company_info.html/clients.html.

So I am looking for something that I can make with an Excel concatenate forumla (which is how I made the above) that will take my two column, match exactly what is in the first column and nothing more, and redirect to exactly what's in the second column.

I read the information that people have helpfully posted and it's very educational but I don't think it addresses this problem. If I have to write a rewrite rule manually for each one, then it would be quicker for me to rewrite everything that's not a page on the new site to a PHP script and do the redirection there. But I thought there would be a simple way to do it with apache.

kjh23

2:13 pm on Jul 10, 2012 (gmt 0)

10+ Year Member



Having had a better read through the apache manual, I have now got my redirects working. I learnt that the redirects are processed in the order that they appear in the configuration file but the redirect diretive is always matched so I sorted my spreadsheet in order of the length of the request URL with the longest first and my existing syntax then worked.

Thanks to those who tried to help.

g1smd

10:20 pm on Jul 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now the question is more clear and what you already tried and how it failed has been explained, the answer is much easier to ascertain.

Yes, redirects must be ordered from most specific to most general. This usually (but not always) means longest first.

I would use
RewriteRule [R=301,L]
syntax rather than
Redirect 301
syntax for this. It's a few extra characters, but the usage of start and end anchors in
RewriteRule
patterns allows for much more fine grained control over what gets redirected.

These three rules each do something very slightly different:
RewriteRule ^foo http://www.example.com/bar [R=301,L]

RewriteRule ^foo/ http://www.example.com/bar [R=301,L]

RewriteRule ^foo/$ http://www.example.com/bar [R=301,L]