.htaccess mod_重写/product/?id={xyz}到/product/{xyz}的问题
我的尝试:.htaccess mod_重写/product/?id={xyz}到/product/{xyz}的问题,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我的尝试: RewriteCond %{QUERY_STRING} ^id=(.*)$ [NC] RewriteRule ^/product$ /product/%1 [NC,L,R=301] 我只想将此规则应用于/product/和/supplier/目录。它们都是一级子目录 注意:product/?id={xxx}实际上是product/index.php?id={xxx}。Apache隐藏了我的扩展和索引。我只想指出这一点 Myproduct/inde
RewriteCond %{QUERY_STRING} ^id=(.*)$ [NC]
RewriteRule ^/product$ /product/%1 [NC,L,R=301]
我只想将此规则应用于/product/
和/supplier/
目录。它们都是一级子目录
注意:product/?id={xxx}
实际上是product/index.php?id={xxx}
。Apache隐藏了我的扩展和索引。我只想指出这一点
Myproduct/index.php
处理给定的参数并确定它应该显示的页面:
index.php
if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) ) {
//html for individual page e.g. /product/?id=foo
//e.g. <h1><?= $_GET['id'] ?> Page</h1>
} else {
//html for product list e.g. /product/ (no parameters)
}
if(isset($\u GET['id'])和&!empty($\u GET['id'])){
//单个页面的html,例如/product/?id=foo
//e、 g.第页
}否则{
//用于产品列表的html,例如/产品/(无参数)
}
在根目录下的一个.htaccess文件中尝试此操作:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} id=(.+) [NC]
RewriteRule ^(product|supplier)/?$ /$1/%1? [NC,L,R=301]
在.htaccess文件中,规则中测试它的URI路径没有前导斜杠(^/product
),因此正则表达式也不能有它。跟踪?
删除传入查询
如果规则集要放在Apache主配置文件中,则应保留前导斜杠:^/(product | supplier)/?$
更新
显示想要的URL,但仍从原始URL获取数据
请求:/product/?id=参数
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(product|supplier)/\?id=([^\s]+) [NC]
# Strip the query and redirect permanently
RewriteRule ^(product|supplier) /$1/%3? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
# Map internally to the original request
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
另一种选择是直接在请求中使用“pretty”URL: 请求
/product/parameter
至/product/?id=parameter
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(product|supplier)/\?id=([^\s]+) [NC]
# Strip the query and redirect permanently
RewriteRule ^(product|supplier) /$1/%3? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
# Map internally to the original request
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(product|supplier)/([^/]+)/? /$1/?id=$2 [L,NC]
您的尝试在一个.htaccess文件中的何处?是的,位于根目录中。为什么要删除传入的GET查询?例如,
product/?id=pickles
=>`product/pickles将给我一个404,而不是显示pickles页面。脚本无法从您请求的新URL获取参数,因为它不包含查询字符串。实现所需功能的唯一方法是重定向到/product/{xyz}
,只是在浏览器的地址栏中显示该URL,同时以静默方式映射回product/?id=pickles
,以允许脚本获取查询字符串。我想这才是你真正需要的。酷。静默映射回会导致URL再次更改回/product/?id=pickles
?如果不是,我怎么做?是的。但是也许可以使用漂亮的URL而不是带有查询的URL。你能检查一下这两个选项的描述,让我知道哪一个适合你的要求吗?更新了我的答案。希望其中一个是你需要的。