简体   繁体   English

.htaccess url重写不能在斜杠后面工作吗?

[英].htaccess url rewrite not working witouth trailing slash?

I want to rewrite the URL of a page in my website. 我想重写网站中页面的URL。 Its basicly really simple. 它基本上很简单。 My original URL looks like this 我的原始网址看起来像这样

http://www.mypage.com/website/page.php?slug=my-page http://www.mypage.com/website/page.php?slug=my-page

I want it to look like this: http://wwww.mypage.com/website/my-page/ 我希望它看起来像这样: http : //wwww.mypage.com/website/my-page/

And that works. 那行得通。 What doesent work is if you remove the trailing slash. 如果删除尾部的斜杠,将起作用。 This is my htaccess: 这是我的htaccess:

RewriteEngine on
RewriteRule ^(.*)/$ page.php?slug=$1 [L]

It seems as if i remove the slash the $_GET['slug'] becomes only page.php but with a trailing slash the variable says "my-page". 似乎我删除了斜杠,$ _ GET ['slug']仅变成page.php,但斜杠末尾的变量显示为“ my-page”。

Is it possible to make it so the link works both without and with trailing slash? 是否有可能使链接在没有斜杠的情况下都能正常工作?

Edit: Does it matter if i have the .htaccess and php file in a childfolder? 编辑:如果我在子文件夹中有.htaccess和php文件,那有关系吗? So my real url is like this: http://www.mypage.com/website/page.php?slug=something 所以我的真实网址是这样的: http ://www.mypage.com/website/page.php?slug= something

I've now edited the post with how it really is. 现在,我对帖子的内容进行了编辑。

RewriteEngine on
RewriteRule ^(.*)/?$ page.php?slug=$1 [L]

The question mark makes the trailing slash optional. 问号使斜杠成为可选。 I would also suggest to have only one kind of URLs. 我也建议仅使用一种URL。 Like redirect URLs without the trailing salsh, to the one with. 就像没有尾随者的重定向URL一样。

I can only think of using this line before RewriteRule ^(.*)/$ page.php?slug=$1 [L] 我只能想到在RewriteRule ^(.*)/$ page.php?slug=$1 [L]之前使用此行

RewriteRule ^(.*)$ page.php?slug=$1 [L]

Basically, in your original code, you declared a trailing slash, so it requires a trailing slash 基本上,在您的原始代码中,您声明了斜杠,因此它需要一个斜杠

Try adding this: 尝试添加以下内容:

RewriteRule ^website/([a-z0-9]+)/?$ website/page.php?slug=$1      [NC,L,QSA]

Amend the regex as needed depending on the type of URLs you want to accept. 根据您要接受的URL的类型,根据需要修改正则表达式。

Look up how htaccess works with regard to things like querystrings and so on: 查找htaccess如何处理诸如查询字符串之类的问题:

http://httpd.apache.org/docs/1.3/howto/htaccess.html http://httpd.apache.org/docs/1.3/howto/htaccess.html

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

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