.htaccess中加入.html扩展名的网址有或没有结尾的斜线 [英] htaccess add .html extension for urls with or without trailing slash
问题描述
于是开始与我有一个自定义URL重写发送一个请求变量的PHP脚本
So to begin with I have a custom url rewrite that sends a request variable to a php script
重写规则如下:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
因此,如果您访问像 domain.com/slug-text
发送塞文
到的index.php
位于文件夹名为test。
So if you access something like domain.com/slug-text
it sends slug-text
to index.php
located in folder named test.
我要的是我所有的网址看起来像 domain.com/slug-text.html
,而塞测试
变量仍应该发送到的index.php
文件。
What I want is all my urls to look like domain.com/slug-text.html
, but slug-test
variable should still be sent to index.php
file.
和
我想不通的是重定向。我希望所有的旧网址从 domain.com/slug-text
或 domain.com/slug-text / $ C $重定向C>到
domain.com/slug-text.html
和塞文
发送到的index.php
文件位于test文件夹。
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text
or domain.com/slug-text/
to domain.com/slug-text.html
and slug-text
sent to index.php
file located in test folder.
搜索到了很多,但无法找到这个问题在互联网上任何地方的答案。
Searched a lot but could not find the answer for this question anywhere on the Internet.
感谢大家的帮助。
更新: 我的新code是:
UPDATE: my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text /
不重定向到 domain.com/slug-text.html
domain.com/slug-text
按预期工作重定向到 domain.com/slug-text.html
domain.com/slug-text/
does not get redirected to domain.com/slug-text.html
domain.com/slug-text
works as intended redirecting to domain.com/slug-text.html
我需要做什么改变吗?
推荐答案
这条规则:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
将捕获 domain.com/slug-text
, domain.com/slug-text /
和 domain.com/slug-text.html
发送塞文
到 /测试/索引。 PHP
在塞
参数。
Will trap domain.com/slug-text
, domain.com/slug-text/
and domain.com/slug-text.html
and send slug-text
to /test/index.php
inside slug
param.
如果你真的想使用重定向 [R = 301]
从旧的URL到新的再使用这样的:
If you really want to redirect using [R=301]
from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
另外请注意,由于使用明确的底部重定向规则被修改,以捕获URL的结尾与的.html
这也是可取的(如果你的.htaccess还没有包含这一点)来过滤现有不要被你重定向规则捕获文件和文件夹的条件。只需在重写规则
行添加这些行:
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule
lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
如果使用符号链接:
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
//此外
.htaccess文件应该是这样的:
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
这篇关于.htaccess中加入.html扩展名的网址有或没有结尾的斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!