views:

77

answers:

1

Hi .htaccess ninjas,

I need help configuring my .htaccess file to handle redirects properly.

Here’s what I need to have happen. Stackoverflow's spam filter wouldn't allow me to post the full domain. So where I say "DOMAIN" you can substitue "domain.com". (I also needed to add and extra t to the http.)

  1. Requests for the DOMAIN/page version of the file should be redirected to www.DOMAIN/page.
  2. Requests for the 'friendly' versions of the URLS should be allowed. So a file that is really at www.DOMAIN/index.php?q=37 should be viewable by going to www.DOMAIN/latest-news
  3. I have a big list of 301 redirects. We recently changed the site from an .asp based CMS to one written in PHP.

Example:

redirect 301 /overview.asp http://www.DOMAIN/overview

Items 1 and 2 are working fine.

However for item 3, if I put in a browser request for "http://www.DOMAIN/overview.asp" instead of redirecting to the friendly name of the file ("http://www.DOMAIN/overview") it will redirect to http://www.DOMAIN/index.php?q=overview.asp. This is the problem.

What do I need to change to get this working right?

My configuration is below:

## Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]

## Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets)/*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]

## For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

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

redirect 301 /overview.asp http://www.DOMAIN/overview
redirect 301 /news.asp http://www.DOMAIN/news
#  ETC....

thanks!

A: 

Mod_rewrite is doing exactly what you're asking it to do ... (yes :-), that's often the problem with computers).

On the /overview.asp http://www.DOMAIN/overview line you're setting the browser to send out a brand new request from scratch, which starts the whole cycle again from the top and gets catched by the ^(.*)$ index.php?q=$1 directive.

Right before this line you should put another RewriteCond to prevent the ^(.*)$ rule to apply if REQUEST_FILENAME is either overview or news. You might also simply rewrite /overview.asp to overview [L] instead of redirecting.

If you can, set the RewriteLog directive to its highest verbosity and look at the logfile - it usually gives very good insights into what's really going on...

EDIT - if I get it right you shoud be doing this:

RewriteCond %{REQUEST_FILENAME} ! \.asp$
RewriteCond %{REQUEST_FILENAME} ! ^overview$
RewriteCond %{REQUEST_FILENAME} ! ^news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

This would prevent any file already ending in .asp, plus those looking for overview and news, to be redirected toward index.php.

I suspect anyway that you got something backwards regarding that SEO stuff. You should indeed start from the structure of the query string that your scripts expect and use that as a base to build a sensible URL addressing schema.

EDIT #2:

There was a space too many between the bang mark ant the regex. The following code doesn't come from memory as the previous - I've tested on my local Apache and it does what's supposed to do (as long as I've understood correctly..)

 RewriteCond %{REQUEST_FILENAME} !\.asp$
 RewriteCond %{REQUEST_FILENAME} !overview$
 RewriteCond %{REQUEST_FILENAME} !news$
 RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Hope this helps

djn
And, BTW, wht are you doing this? The common SEO practice in URL rewriting is to have the world at large send in requests for the friendly URL and then rewrite it into a real filename, not the other way round.
djn
Bezly
How can I do the the RewriteCond to NOT apply if the request coming in is for a '.asp' file? I have about 15 301 redirects and they are all .asp's.
Bezly
Hi, I tried this and got a 500 error. I think we're going down the right path though...
Bezly