Zootyo
Zootyo

Reputation: 160

Redirecting URL with .htaccess

I want to redirect URL, using .htaccess. I have the following code:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?action=$1&id=$2

This is working correctly if I don't use dot in the URL. But if I want to access for example this URL:

load/com.example.unique.id

then I will get error 404. Of course, I can understand why is this: I haven't include the dot in my expression. However if I include it, I will get error 500.

I tried to include dot in the expression like this:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-\.]+)$ index.php?action=$1&id=$2

Any idea why is this happening? Thanks!

Upvotes: 1

Views: 73

Answers (2)

Andrew
Andrew

Reputation: 5340

I think the problem is with the '-' in [a-zA-Z0-9_-.], try this:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_\-.]+)$ index.php?action=$1&id=$2

Upvotes: 2

anubhava
anubhava

Reputation: 786291

You can use dot but hyphen needs to be last or at first position in character class to avoid escaping. Correct regex based rule will be like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w.-]+)/([\w.-]+)/?$ index.php?action=$1&id=$2 [L,QSA]

Upvotes: 1

Related Questions