如何解决使用.htaccess中的Mod Rewrite删除php扩展名和参数,以创建两个seo友好的url
我已经阅读了很多有关htaccess重写的主题,但是努力使任何东西都能按需工作。
我在这里旋转了几个小时。
我的文件夹中有两个页面。
/proposal/index.php
/proposal/dl.php
因此普通查询看起来像这样。
/ proposal /?code = 123abc
/proposal/dl.php?code=123abc。
但是我想这样发送给用户:
/ proposal / 123abc /
/ proposal / dl / 123abc /
有人可以帮我吗?
TIA
注意 我确实尝试了下面的代码,该代码可以完美地在index.php上运行,但对dl.php无效(如果我将dl.php添加到混合中)
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
解决方法
这可能是您要寻找的变体。这两个规则都捕获嵌入在请求的URL中的实际参数,以在内部重写的请求中重新使用它。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteRule ^dl/([^/]+)/?$ dl.php?code=$1 [END]
RewriteRule ^([^/]+)/?$ index.php?code=$1 [END]
您可能还希望添加重定向规则,以使用“旧” URL重定向客户端:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteCOnd %{QUERY_STRING} ^code=([^/]+)$
RewriteRule ^dl\.php$ dl/%1 [R=301,END]
RewriteRule ^index\.php$ %1 [R=301,END]
RewriteRule ^$ /%1 [R=301,END]
RewriteRule ^dl/([^/]+)/?$ dl.php?code=$1 [END]
RewriteRule ^([^/]+)/?$ index.php?code=$1 [END]
为此,最好从302重定向开始,然后在一切按预期方式工作时才将其更改为301。
两个变体都使用相对路径,以便规则在比http服务器的文档根目录更深的分布式配置文件中工作。我个人更喜欢在实际的主机配置中实施此类规则,而不是出于各种原因而使用分布式配置文件(“ htaccess”)。
,最后,这奏效了。
RewriteEngine On
RewriteRule ^dl/(.*) dl.php/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
现在我可以像这样调用页面了:
/ proposal / 123abc /
/ proposal / dl / 123abc /
我所做的就是将以下行添加到原始代码中。
RewriteRule ^dl/(.*) dl.php/$1 [L]
我的子目录的结构如下:
/proposal/.htaccess
/proposal/index.php
/proposal/dl.php
现在,我能够为这两个文件的客户端提供漂亮的URL,并使用以下参数调用它们的内容:
/ proposal / 123abc
/ proposal / dl / 123abc
通常会这样的
/proposal/index.php?code=123abc或/ proposal /?code = 123abc
/proposal/dl.php?code=123abc
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。