添加尾随斜线的mod_rewrite [英] Add a trailing slash mod_rewrite

查看:115
本文介绍了添加尾随斜线的mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道我是怎么在使用mod_rewrite我的网址的结尾处添加一个尾随斜线?

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

我的网址显示,像这样:

My URL show like so:

WWWW。 ****** 的.com /页面名称

wwww.******.com/pageName

我想让它显示像这样:

WWWW。 ****** .COM /网页/

wwww.******.com/pageName/

该URL内部拿着一个GET请求,但我希望它看起来像一个真正的目录。

The URL is holding a GET request internally, but I want it to look like a genuine directory.

推荐答案

您可以轻松地斜线添加到客户端看到的网址,但你必须考虑到,在这之后,你得到的请求斜线你重定向浏览器。

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 规则将有页面名称参数在它尾部斜杠。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆