简体   繁体   English

mod_rewrite添加斜杠并传递给index.php

[英]mod_rewrite add trailing slash and pass to index.php

When I request my website, I'm trying to add trailing slash to the URL and then pass it to index.php as parameter using mod_rewrite. 当我请求我的网站时,我试图在URL末尾添加斜杠,然后使用mod_rewrite将其作为参数传递给index.php。

My .htaccess file looks like this: 我的.htaccess文件如下所示:

RewriteEngine On    

#Add trailing slash
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R]

#Pass to index.php
RewriteRule ^(.*)/$ index.php?p=$1

And in my index.php file just outputs the parameter: 在我的index.php文件中,仅输出参数:

<?php echo $_GET["p"]; ?>

But when I type to the adress bar anything except http://mydomain.com/ , for example http://mydomain.com/contact , the php always outputs http://mydomain.com/index.php . 但是,当我在地址栏键入http://mydomain.com/以外的任何内容(例如http://mydomain.com/contact)时php始终会输出http://mydomain.com/index.php So http://mydomain.com/index.php was somehow passed as the parameter instead of the requested page such as contact , but I don't know why... 因此,以某种方式将http://mydomain.com/index.php作为参数而不是诸如contact之类的请求页面进行了传递,但是我不知道为什么...

Also, when I edit 另外,当我编辑

RewriteRule ^(.*)/$ index.php?p=$1

to

RewriteRule ^(.*)/$ /index.php?p=$1

and I type URL eg mydomain.com/contact Apache gives me 302 Found and a link to where it was moved, but it links to the same page... 然后输入URL,例如mydomain.com/contact Apache给了我302 Found以及指向它的移动位置的链接,但是它链接到同一页面...

Any Ideas? 有任何想法吗?

Thanks. 谢谢。

Apache has a nice long writeup for this specifically. Apache为此专门编写了很长的文章。

Trailing Slash Problem 尾部斜杠问题

Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. 每个网站管理员都可以唱一首关于目录引用URL末尾斜杠问题的歌曲。 If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. 如果缺少它们,服务器将转储错误,因为如果您说/〜quux / foo而不是/〜quux / foo /,则服务器将搜索名为foo的文件。 And because this file is a directory it complains. 而且由于该文件是目录,因此会抱怨。 Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. 实际上,在大多数情况下,它会尝试自行修复它,但是有时您需要模仿此机制。 For instance after you have done a lot of complicated URL rewritings to CGI scripts etc. Solution: 例如,在您对CGI脚本等进行了许多复杂的URL重写之后。解决方案:

The solution to this subtle problem is to let the server add the trailing slash automatically. 解决这个细微问题的方法是让服务器自动添加尾部斜杠。 To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. 要正确执行此操作,我们必须使用外部重定向,以便浏览器正确地请求后续图像等。如果仅进行内部重写,则此操作仅适用于目录页面,但如果该页面中包含任何图像,则会出错使用相对网址,因为浏览器会请求一个内联对象。 For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect! 例如,在没有外部重定向的情况下,/~quux/foo/index.html中对image.gif的请求将变成/~quux/image.gif!

So, to do this trick we write:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]

The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. 疯狂的和懒惰的甚至可以在其homedir的顶级.htaccess文件中执行以下操作。 But notice that this creates some processing overhead. 但是请注意,这会产生一些处理开销。

RewriteEngine  on
RewriteBase    /~quux/
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$           $1/  [R]

It can be found here: 在这里能找到它:

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

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

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