Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Redirect help

         

solikesimon

5:34 am on May 8, 2012 (gmt 0)

10+ Year Member



I'm trying to do a .htaccess Redirect that looks like:
Redirect /foo www.siteurl.com/bar.html
But when I try to access www.siteurl.com/foo it brings up the URL www.siteurl.com/bar.html?q=foo which shows my initial page /foo and not my redirected page.

my .htaccess looks like:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*)$ SITEURL/$1 R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I'm guessing the last line is what is doing it, but that line needs to be there to enable friendly url's for the CMS (Etomite).

I guess I want the redirect to ignore the rewrite rule if that's possible?

Thanks!

lucy24

7:44 am on May 8, 2012 (gmt 0)

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



Huh. You're using mod_rewrite syntax with a mod_alias directive. Usually I see the opposite.
Redirect /foo www.siteurl.com/bar.html

But when I try to access www.siteurl.com/foo it brings up the URL www.siteurl.com/bar.html?q=foo which shows my initial page /foo and not my redirected page.

I don't understand what you are saying here. If you request /foo and instead end up at /bar then you are getting redirected. But where's the query string coming from?

RewriteBase, if needed, has to go before all your rewrites. Here it is not needed because / is the default.

The form
Redirect /foo www.siteurl.com/bar.html

means: take any request containing the element /foo, replace it with www.example.com/bar.html, reappend anything that came after /foo, and issue a 302 Redirect to this new URL. This is one of several important differences between mod_alias and mod_rewrite.

But this need not concern you, because you are going to dump that Redirect and replace it with a RewriteRule. It will look like this:

RewriteRule {pattern here} http://www.example.com/{target here} [R=301,L]

The QSA flag is only necessary when you have added a query but still want to keep the old one. This is not common.

RewriteRule ^(.*)$ SITEURL/$1 R=301,L]

Can we stipulate that you've got a glaring typo in there? :) But again it doesn't matter, because you are also going to dump the "SITEURL".

solikesimon

10:19 pm on May 8, 2012 (gmt 0)

10+ Year Member



Hi Lucy,

Thank you for your reply.

You are correct - SITEURL should be www.siteurl.com.

I've replaced my Redirect line with the following line. But I'm not using a pattern I'm using specific values because it only needs to happen in a handful of situations.

RewriteRule foo [sitename.com...] [R=301,L]

I'll use this perhaps 4 times using my specific pages to redirect.

This should be okay?

Thanks again!

lucy24

11:36 pm on May 8, 2012 (gmt 0)

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



:) "Pattern" is a technical term. It simply means "the piece on the left". If you're redirecting one specific page, the "pattern" could be

^/directory/filename.html$

It's still called a pattern.

Note that if you say

foo

without anchors, you will be redirecting any request that contains the sequence of letters "foo" anywhere at all. (See unrelated thread elsewhere about domains that accidentally contain the sequence of letters s,#,x.) This is fine if you only have one page containing that sequence. Otherwise you may need anchors.

solikesimon

11:53 pm on May 8, 2012 (gmt 0)

10+ Year Member



Right excellent. I've put the anchor $ in and now everything seems to be fine.

Thanks so much =)