Randheer Singh Chouhan
Randheer Singh Chouhan

Reputation: 163

Dynamic URL rewriting in htaccess

I've a url like :

http://localhost/project/gallery_detail.php?id=3

what I want here is :

http://localhost/project/USERNAME/3

What I'm trying is :

RewriteEngine 
OnRewriteRule /(.*)/(.*)/$ gallery_detail.php?id=$1

and handling this to php file but didn't succeed.

Upvotes: 1

Views: 878

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteRule /(.*)/(.*)/$ gallery_detail.php?id=$1

In per-directory .htaccess files, the URL-path never starts with a slash. You are also only capturing 2 groups, when you need 3 and are using the first (ie. project) in the substitution.

Try something like the following instead, if your .htaccess file is located in the document root (ie. localhost/.htaccess).

RewriteRule ([^/]+)/[^/]+/(\d+)$ $1/gallery_detail.php?id=$2 [L]

You don't need to capture groups if you don't need them. [^/]+ is for 1 or more characters, other than slash (the directory separator).

UPDATE: However, if the .htaccess file is located in the /project subdirectory then change this to:

RewriteRule [^/]+/(\d+)$ gallery_detail.php?id=$1 [L] 

Upvotes: 2

Related Questions