简体   繁体   English

htaccess中的动态URL重写

[英]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. 并将其处理为php文件,但未成功。

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

In per-directory .htaccess files, the URL-path never starts with a slash. 在每个目录的.htaccess文件中,URL路径从不以斜杠开头。 You are also only capturing 2 groups, when you need 3 and are using the first (ie. project ) in the substitution . 当需要3个组并使用替代中的第一个(即project )时,您也仅捕获2个组。

Try something like the following instead, if your .htaccess file is located in the document root (ie. localhost/.htaccess ). 如果您的.htaccess文件位于文档根目录(即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). [^/]+用于1个或多个字符,而不是斜杠(目录分隔符)。

UPDATE: However, if the .htaccess file is located in the /project subdirectory then change this to: 更新:但是,如果.htaccess文件位于/project子目录中,则将其更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM