Welcome to WebmasterWorld Guest from 54.158.55.251

Forum Moderators: Ocean10000 & incrediBILL & phranque

301 redirects not working on new server

rewrite rules working fine

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

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Oct 24, 2003
posts: 590
votes: 4


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]

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

Moderator from US 

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

joined:Sept 26, 2001
posts:8647
votes: 385


Depends how the server is set up.
4:25 am on Mar 5, 2017 (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:10859
votes: 67


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
4:26 am on Mar 5, 2017 (gmt 0)

Administrator from US 

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

joined:Dec 27, 2006
posts:3278
votes: 160


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.
4:29 am on Mar 5, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Oct 24, 2003
posts: 590
votes: 4


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...
5:49 am on Mar 5, 2017 (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:13744
votes: 462


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.
6:32 am on Mar 5, 2017 (gmt 0)

Administrator from US 

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

joined:Dec 27, 2006
posts:3278
votes: 160


@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. (?)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members