chris
chris

Reputation: 461

mod rewrite help

I am trying to use mod rewrite to remove and replace part of my url. I am looking to get my urls looking like this.

http://domain.com/e813c697e8dd8dc2bbfecb1d20b15783.html

instead of

http://domain.com/lookup.php?md5=e813c697e8dd8dc2bbfecb1d20b15783

lookup.php calls matches the md5 to the database and fetches and forwards you to the correct url. All I need to do now is rewrite it so that it rewrites from this

http://domain.com/lookup.php?md5=e813c697e8dd8dc2bbfecb1d20b15783

to this

http://domain.com/e813c697e8dd8dc2bbfecb1d20b15783.html

I have tried this which works but it makes rewrites from any .html page at root level and makes it display nothing "blank".

RewriteRule ^([a-z0-9]+)\.html$ /lookup.php?md5=$1 

Can anyone tell me a way to do this so that my regular html pages are not messed up and be able to display these links how I want to? Thanks.

Upvotes: 0

Views: 531

Answers (3)

icyrock.com
icyrock.com

Reputation: 28598

One thing you can do is quantify the number of hex digits:

RewriteRule ^([0-9a-f]{32})\.html$ /lookup.php?md5=$1 

as md5 will always have 32 hex digits.

Upvotes: 1

LazyOne
LazyOne

Reputation: 165118

Your current rule is a way too broad. You need to make it more specific to only match md5 hash value -- which is easy:

RewriteRule ^([a-f0-9]{32})\.html$ /lookup.php?md5=$1 [QSA,L]
  1. Your pattern for file name is too broad -- it will match any file with letters and digits. md5 hash, on another hand, uses very limited subset of characters (a-f only) and digits .. and has to be 32 characters long. This pattern ([a-f0-9]{32}) does the job perfectly.

  2. I have also added L and QSA flags (QSA to preserve any existing query string (like, tracking info, for example) and L to stop matching any other rules).

  3. To further ensure that it does not match any real files which may have name in such format (who knows), add RewriteCond %{REQUEST_FILENAME} !-f line before the rule.

Upvotes: 1

Paul
Paul

Reputation: 141839

That depends on the naming scheme of your regular html pages. For starters though you could change it to: RewriteRule ^([a-f0-9]+)\.html$ /lookup.php?md5=$1

Which would make any html pages which have a letter not between a and f work.

If none of your HTML pages have numbers in their names, and if the cost of a page not redirecting outweighs the odds of an md5 hash having no numbers in it. You could check that there is at least one digit in the filename: RewriteRule ^([a-f]*\d[a-f\d]+)\.html$ /lookup.php?md5=$1

Lastely if it is acceptable for you to have urls like http://domain.com/md5/e813c697e8dd8dc2bbfecb1d20b15783.html instead you could change it to: RewriteRule ^md5/([a-f0-9]+)\.html$ /lookup.php?md5=$1 and not have to worry about the fragileness of the other methods.

I'm not sure, but I think you have a RewriteBase / somewhere in you .htaccess judging by your example mod_rewrite. If not you might need to add a / right after the ^ in whatever RewriteRule you choose to go with.

Upvotes: 0

Related Questions