2

I have a problem with my .htaccess redirects. I have both a rewrite rule to remove the "index.php?/" from my URLs (I'm using a PHP framework), and also 301 redirects redirecting from old pages:

# START THE REWRITE
RewriteEngine on
Options -Indexes
Options +FollowSymLinks
RewriteCond $1 !^(index\.php|assets)
RewriteRule ^(.*)$ /index.php?/$1 [L]

# 301 REDIRECTS
Redirect 301 /oldpage.html http://www.example.com/old_page
Redirect 301 /page.html http://www.example.com/page
Redirect 301 /page-that-no-longer-exists.html http://www.example.com/

I can't rename my .html files with another rewrite rule as some of the pages don't match the new pages, and some other .html pages are redirecting to the home page - hence the 301 redirects.

And so, because of the rewrite rule, the first 301 redirect will direct to

http://www.example.com/old_page?/old_page.html

instead of

http://www.example.com/old_page

I have tried reordering the contents of the .htaccess file (which does nothing), setting the rewrite to "off" after the rewrite (which stops the 301's working) and writing specific rewrite rules for each 301 (none of which worked).

I'm sure i'm missing something. Cheers.

Nick Pyett
  • 3,338
  • 1
  • 23
  • 27
  • I'm trying to determine if you answered your own question at the bottom here. Is that the case? You should answer your question down below and leave the contents of your question alone so people don't think you still need help. – Michael Irigoyen Jan 27 '11 at 16:29
  • @Micahel Irigoyen. Ok done. I left it open because the proposed solutions are only really work arounds, a solution that doesn't mean changing my RewriteCond would be better, but it is sorted for my situation. Cheers. – Nick Pyett Jan 28 '11 at 09:10

2 Answers2

1

OK, I've come up with a couple of solutions.

The first is to send the redirect in the example to

Redirect 301 /oldpage.html http://www.example.com/index.php?/old_page

This does not apply the rewrite removing the "index.php?", but it does work.

The second work around is to edit the rewrite conditions to exclude any .php or .html files:

RewriteCond $1 !^([a-zA-Z1-9\-\_/]*\.php$|assets|[a-zA-Z1-9\-\_/]*\.html$)

This may also exluce any URLs that end in .php or .html, meaning the rewrite won't work, and your framework will break, but as long as you stay away from that you should be ok.

Nick Pyett
  • 3,338
  • 1
  • 23
  • 27
0

Your issue is that this line: RewriteRule ^(.*)$ /index.php?/$1 [L] forces a query parameter in that rewrite. Some hosts are setup this way, one I know of in particular is DreamHost. So, anything that is redirected appears broken.

You can try this

RewriteRule ^dir/file.php$ http://webpage/newdir/file2.php [R=301,L]

Dan
  • 3,338
  • 5
  • 36
  • 58