添加尾部斜杠 mod_rewrite [英] Add a trailing slash mod_rewrite
问题描述
只是想知道如何使用 Mod_Rewrite 在 URL 末尾添加斜杠?
just wondering how I add a trailing slash at the end of my URLs using Mod_Rewrite?
这是我目前的 .htaccess 文件:
This is my .htaccess file currently:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?pageName=$1
我的网址显示如下:
www.******.com/pageName
wwww.******.com/pageName
我希望它显示如下:
www.******.com/pageName/
wwww.******.com/pageName/
该 URL 在内部持有一个 GET 请求,但我希望它看起来像一个真正的目录.
The URL is holding a GET request internally, but I want it to look like a genuine directory.
推荐答案
您可以轻松地在客户端看到的 URL 中添加尾部斜杠,但您必须考虑在您收到的请求中的尾部斜杠你已经重定向了浏览器.
You can easily add a trailing slash to the URL that a client sees, but you'll have to take into account that trailing slash in requests that you get after you've redirected the browser.
所以重定向可能看起来像这样:
So the redirect could look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
你会想要它在上面:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?pageName=$1
规则集,因为重定向需要在路由到 index.php
之前发生.请注意,当浏览器被重定向到以 /
结尾的 URL 时,您的 index.php
规则将具有带有尾随的 pageName 参数斜线.
ruleset, because the redirect needs to happen before the routing to index.php
. Note that when the browser gets redirected to a URL that ends with a /
, your index.php
rule will have the pageName param with a trailing slash in it.
这篇关于添加尾部斜杠 mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!