mod_rewrite using the exact same urls


I have just made a central dynamic script to replace a messy folder full of static files. However, I would like to preserve the old URL’s without having to delete those files. Let me give you an example:

I have the following files:

  1. /oldpages/projekt1/index.html
  2. /oldpages/projekt2/index.html
  3. /oldpages/projekt3/index.html

The actual new urls are:

  1. /newscript/script.php?name=projekt1
  2. /newscript/script.php?name=projekt2
  3. /newscript/script.php?name=projekt3

The rewrite rule is:

  Options -MultiViews -Indexes +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^oldpages/([A-Za-z0-9_\\-]+)/(index.html)?$ /newscript/script.php?name=$1 [L,QSA]

However, whenever I try to access it just keeps giving back the old files. After googling, everyone said that multiviews may be the culprit, but apparently, that’s not fixing it.

The rule works if the directory doesn’t exist on the server, the following works perfectly:

Options -MultiViews -Indexes +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^newpages/([A-Za-z0-9_\\-]+)/(index.html)?$ /newscript/script.php?name=$1 [L,QSA]

And gets the right page.

What am I doing wrong?

Any help would be greatly appreciated.

Hi JW!

First, your title is incorrect. A URL is the full location of the request.

You can replace all the old URIs with a “handler script” (as you have done) and then DELETE the old files.

You have CORRECTLY (IMHO) disabled MultiViews.

RewriteBase is meant to UNDO a mod_alias Redirect so it’s not required (and can mess-up your redirections).

You’ve changed directory levels to watch out for your support files as they’re liable to “go missing.”

You’ve redirected index.html (which is the same in every instance) but you don’t need to. Okay, this is only optimization so it’s not a big deal. Unfortunately, you’ve made the / mandatory which may lead to 404’s.

The leading / in the redirection CAN cause a problem as you’re telling Apache to look first to its root directory.

If you use the R=301 flag, SE’s can update their databases.

Options -MultiViews -Indexes +FollowSymLinks

RewriteEngine On
[COLOR="Red"]RewriteBase /[/COLOR]
RewriteRule ^oldpages/([A-Za-z0-9_\\-]+)([B]/[/B]index.html)?$ [COLOR="Red"]/[/COLOR]newscript/script.php?name=$1 [[COLOR="Blue"]R=301,[/COLOR]L,QSA]

While it sounds like I’m picking on you, your code is actually very good! A little optimization and a correction here and there can make it (technically) perfect.



Hello dklynn,

Actually, not at all. I always loved tough teachers. :slight_smile:

The RewriteBase, it’s an old habit. One of the main hosts that I am dealing with (1und1) had a very quirky mod_rewrite support and RewriteBase always needs to be there.

The problem seems to have been an old htaccess in the /oldpages directory with RewriteEngine On (for some simple 301 redirects). I never noticed it’s there and that was the source of the problem as it apparently was rewriting the rules in the root htaccess.

I didn’t add the R=301 because I wanted the transition to be smooth and relatively invisible. If I can preserve the same urls, why should I change them to strange ones with question marks and ampersands? I think that those are pretty urls at no additional expense.

Should I add a rule to redirect /newscript/script.php?name=projekt1 to /oldpages/projekt1/index.html?

Thanks again! You can pick on me anytime you want. :slight_smile:

I’m assuming there is no link anywhere on the internet yet to newscript.php because it’s “hidden” behind (or, masking as) the /oldpages directory ?

If so, I wouldn’t bother. If no one knows the new link, why redirect it to the old link? All that does is burn cpu cycles for nothing :slight_smile: