Forum Moderators: phranque

Message Too Old, No Replies

301 redirects not working on new server

rewrite rules working fine

         

ichthyous

3:39 am on Mar 5, 2017 (gmt 0)

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



I recently changed hosting companies and my old htaccess file has stopped working for some reason. I finally located the problem, the 301 redirect rules are causing the site to redirect, while the rewrite rules work fine. For example:

Redirect 301 "/pages/example_page.html" http://example.com/pages/example-page/ - not working
RewriteRule ^(pages/)?old-example-page-url/$ http://example.com/example-page/ [R=301,L]
- working fine

Has anyone encountered this problem where one type works and the other doesn't? Any help appreciated!



[edited by: not2easy at 3:54 am (utc) on Mar 5, 2017]
[edit reason] readability edit [/edit]

keyplyr

4:04 am on Mar 5, 2017 (gmt 0)

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



Depends how the server is set up.

phranque

4:25 am on Mar 5, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Redirect is a mod_alias directive and RewireteRule is a mod_rewrite directive.
it is possible that mod_alias is not enabled on your server.

change the Redirect directive to an equivalent RewriteRule syntax and you should avoid this problem and other problems in the future.
you should never mix the two in the same configuration.

http://httpd.apache.org/docs/current/rewrite/avoid.html [httpd.apache.org]:
when there are Redirect(Match) and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file

not2easy

4:26 am on Mar 5, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The first line of code is using mod_alias and the second line uses mod_rewrite. Try converting the first rule to use the mod_rewrite format and add the conditions for each rule. Best practice is not to combine the use of both modules because they do not always follow the order they appear to be listed. Most htaccess files have additional rules that are using mod_rewrite for canonicalization (www / non www) for example.

It helps if you include a description of what each rule is supposed to be doing, to help others spot conflicts and provide accurate help.

Here are a couple of past discussions on the subject that might help clarify the reasons for using the mod_rewrite module if possible.
from: [webmasterworld.com...]
Note that if you are already using RewriteRule (mod_rewrite) then you probably shouldn't be trying to use RedirectMatch (mod_alias). The mod_rewrite directives are likely to take priority, regardless of their order.

from: [webmasterworld.com...]
Any rule that creates a redirect goes before the rules that create rewrites alone. If it isn't your own server, the only way you can be sure this happens is by doing everything in the same module.

ichthyous

4:29 am on Mar 5, 2017 (gmt 0)

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



Thanks Phranque I've heard that before but I could not get these rules working using mod rewrite since many of the urls being redirected contain spaces, as in:

Redirect 301 "/tags/example page" http://example.com/tags/example-page/

These don't work with url rewrite as far as I know...

lucy24

5:49 am on Mar 5, 2017 (gmt 0)

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



Why wouldn't they work? Just be sure to escape any literal spaces. (Quotation marks may or may not work. A backslash \ definitely will.) Spaces almost always have syntactic meaning in Apache, so that's pretty much a universal rule.

I'm sure you have already been told that using spaces in URLs is a terrible idea, so I hope the redirect means that you're getting rid of them all :)

Weird for shared hosting not to allow mod_alias redirects, though...

Edit: Wow, not2easy, it was fun reading through those old threads. But also a little horrifying, because at least two different places I said $1 in a target where clearly it was supposed to be $2. Urk.

not2easy

6:32 am on Mar 5, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



@lucy24 -Corrected, thanks for the heads-up. ;)

The host may allow mod_alias, but there may be mod_rewrites in places that affect the result of mod_alias or vice-versa. (?)