Welcome to WebmasterWorld Guest from 54.166.133.84

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

301 Redirect not working

ReWriteRule working great but can't get redirect to work

     
7:57 pm on Dec 23, 2011 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts: 32
votes: 0


I recently restructured my site and can't get the redirects to work in my .htaccess file. The existing code for rewriting works fine, my code is:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

(sample rewrite)
RewriteRule ^example/([^/]+)$ example-2.cfm?example=$1 [L]
(big list of rewrites after this)


No problem, but then I enter redirects and they don't work. I've tried a number of formats including:

Redirect 301 example.cfm example-2.cfm
Redirect /example.html http://example.com/example-2.cfm
Redirect 301 /example.cfm http://www.example.com/example-2.cfm


And so forth. Is it something weird like where I'm placing the code? (after ReWriteEngine On)

Kinda lost with this stuff so any help greatly appreciated.
9:16 pm on Dec 23, 2011 (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


You need to use a RewriteRule for each one and they will each need a preceding RewriteCond looking at THE_REQUEST, not QUERY_STRING, to prevent an infinite redirect-rewrite loop. There's several hundred previous threads with example code to get you started here.

[edited by: g1smd at 9:58 pm (utc) on Dec 23, 2011]

9:27 pm on Dec 23, 2011 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts: 32
votes: 0


Thanks for the reply, I'll search around for RewriteCond. My only question is if I'm just using a RewriteRule instead of redirecting, isn't that just keeping the old url alive? I want to transition to a better format going forward.
10:01 pm on Dec 23, 2011 (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


RewriteRules can be used for rewriting a request so that content is fetched from a different location inside the server or can be used for redirecting a request to a different URL such that the browser then makes a new request for that other URL. It's all in what you specify as the target (either an internal server path or a URL) and which combination of flags (especially R=301 and L) you use. Remember, RewriteRules act on requests only after a link is clicked, so you need to publicise in links the URLs that you want users to see and use.
12:18 am on Dec 24, 2011 (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:15490
votes: 743


mod_alias generally executes after mod_rewrite. That means that your Redirects (by that name) don't see what the user originally requested, they see the result of the rewrite. This is only one of many reasons not to mix mod_rewrite and mod_alias.

Redirects need to happen before rewrites. Do them with mod_rewrite in the form

RewriteRule {blahblah} {otherblahblah} [R=301,L]

If necessary, add a RewriteCond looking at %{THE_REQUEST} so the only things that get redirected are the ones that have not already been rewritten.

You may also need to read up on query strings. Do a forums search and you will find a standard piece of boilerplate.
12:53 am on Dec 24, 2011 (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


mod_alias generally executes after mod_rewrite

mod_alias often executes before mod_rewrite, but where mod_alias executes after mod_rewrite the results are fatal for SEO.

Most site owners have no control over how this part of the server configuration has been set by their hosting company so the only safe option is to use RewriteRule for all of the rules and list RewriteRules that redirect before RewriteRules which rewrite.
2:51 am on Dec 24, 2011 (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:15490
votes: 743


Well, that was unnerving. The two threads got consolidated at the precise instant I hit Preview so I had to run around and find the other one :)

Most site owners have no control over how this part of the server configuration has been set by their hosting company

You can find out by simple experiment. (Mine goes SetEnvIf, Rewrite, Alias in that order.) I was thinking if you install them in alphabetical order, then you're always going to get mod_alias executing near the end.

the results are fatal for SEO

Possibly even fatal for the site itself ;) There's a joke in there somewhere, but I can't remember it.


:: memo to self: investigate what order MAMP did its stuff in ::