简体   繁体   English

htaccess为网址加上.html扩展名(带斜线或不带斜线)

[英]htaccess add .html extension for urls with or without trailing slash

So to begin with I have a custom url rewrite that sends a request variable to a php script 所以首先,我有一个自定义的URL重写,它将请求变量发送到php脚本

Rewrite rule is below: 重写规则如下:

RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]

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-textslug-text ,它将把slug-text发送到名为test的文件夹中的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. 我想要的是所有URL都看起来像domain.com/slug-text.html ,但是slug-test变量仍应发送到index.php文件。

And

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. 我希望所有旧的URL从domain.com/slug-textdomain.com/slug-text/重定向到domain.com/slug-text.html并将slug-text发送到位于测试文件夹中的index.php文件。

Searched a lot but could not find the answer for this question anywhere on the Internet. 进行了大量搜索,但是在Internet上的任何地方都找不到该问题的答案。

Thank you all for the help. 谢谢大家的帮助。

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/ does not get redirected to domain.com/slug-text.html domain.com/slug-text works as intended redirecting to domain.com/slug-text.html domain.com/slug-text/不会重定向到domain.com/slug-text.html domain.com/slug-text可以按预期的方式重定向到domain.com/slug-text.html

What do i need to change? 我需要更改什么?

This rule: 这条规则:

RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]

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. 将陷阱domain.com/slug-textdomain.com/slug-text/domain.com/slug-text.html和发送slug-text/test/index.php里面slug PARAM。

If you really want to redirect using [R=301] from old urls to new then use this: 如果您确实想使用[R=301]从旧网址重定向到新网址,请使用以下命令:

RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]

Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html 还要注意,由于使用了显式重定向,底部规则已被修改以捕获以.html结尾的url

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. 还建议(如果您的.htaccess中尚未包含此内容)为不被重定向规则捕获的现有文件和文件夹筛选条件。 Simply add these lines before RewriteRule lines: 只需在RewriteRule行之前添加以下行:

# 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

// addition //加法
Your .htaccess file should look like this: 您的.htaccess文件应如下所示:

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]

This is supposed to redirect /slug-text to /slug-text.html 这应该将/ slug-text重定向到/slug-text.html

RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html 

This is in the case when slug-text is only letters, digits, – and _. 当“条状文字”仅是字母,数字,-和_时,就是这种情况。 Rewrite slug-text.html to a php file and pass the slug as a param: 将slug-text.html重写为php文件,并将slug作为参数传递:

RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L] 

If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request. 如果您的.htaccess中都有这两行,则第一个将执行从旧URL到新URL的重定向,第二个将处理请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM