Kronos
Kronos

Reputation: 193

Whats wrong with this RewriteRule for .htaccess

I want to rewrite my url:

localhost/users/045da557b7

to

localhost/users/index.html?userID=045da557b7

here is my .htaccess

RewriteEngine On
RewriteRule ^/([0-9a-z]{10})/$ /index.html?userID=$1 [L]

But the browser gives me 404 back.

I have the rewrite mod uncommented in the httpd.conf. Thank you for your help.

Upvotes: 1

Views: 43

Answers (2)

Marc B
Marc B

Reputation: 360862

^/([0-9a-z]){10}/$
a                b
   cccccccc dddd

a - anchor pattern to start of string
b - anchor pattern to end of string
c - allow numbers and lower case alphabet
d - must have exactly 10 characters


users/045da557b7

16 characters  (past your limit of 10)
contains a / - not in your list of allowed characters

You've basically said "allow at most 10 charccters of 0-9a-z", then passed in 17 characters and include characters that aren't allowed. So your entire pattern will refuse to match.

Upvotes: 0

anubhava
anubhava

Reputation: 786091

You need to use proper RewriteBase by placing this rule in /users/.htaccess:

RewriteEngine On
RewriteBase /users/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z]+)/?$ index.html?userID=$1 [L,QSA,NC]

Upvotes: 1

Related Questions