简体   繁体   English

mod_rewrite:添加尾部斜杠?

[英]mod_rewrite: add trailing slash?

I am using a .htaccess file to direct requests for a directory to a custom php file that provides info about that directory (and i want the url displayed by the browser to not change). 我正在使用.htaccess文件将目录请求定向到自定义php文件,该文件提供有关该目录的信息(我希望浏览器显示的URL不会更改)。

Here is the relevant part of the .htaccess file. 这是.htaccess文件的相关部分。

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L] 

This works well if you go to a directory and include a trailing slash: 如果您转到目录并包含尾部斜杠,这很有效:

http://domain.com/path/

But without the trailing slash, it doesn't 但没有尾随斜线,它没有

http://domain.com/path

The url (shown in the browser) turns into: 网址(在浏览器中显示)变为:

http://localhost:8888/path/?url=/path

I've tried fixing this by adding a rule above this rule: 我试过通过在此规则之上添加规则来修复此问题:

RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]

But that isn't working for me. 但那不适合我。

How can I get .htaccess to add the trailing slash if it is omitted and then act just as if it had been there? 如果省略了尾部斜杠,我怎么能得到.htaccess来添加尾部斜杠,然后就好像它已经存在一样?

Thank you 谢谢

Update: As requested, here is the whole thing. 更新:根据要求,这是完整的事情。

<IfModule mod_rewrite.c>
RewriteEngine On

#force trailing slashes on real directories
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]

#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L]  

</IfModule>

This line: 这一行:

RewriteCond %{REQUEST_FILENAME} -D

Should have been: 本来应该:

RewriteCond %{REQUEST_FILENAME} -d

Don't bother with Mod Rewrite, there is a directive for it 不要打扰Mod Rewrite,它有一个指令

<Location /some/path>
  DirectorySlash On
  SetHandler some-handler
</Location>

http://httpd.apache.org/docs/2.2/mod/mod_dir.html http://httpd.apache.org/docs/2.2/mod/mod_dir.html

Try this. 尝试这个。 It will add trailing slash to directory requests which don't have trailing slash. 它会向没有尾部斜杠的目录请求添加尾部斜杠。

<IfModule mod_rewrite.c>
RewriteEngine On

#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?)/?$ /myphp.php?url=$1/ [QSA,L]  

</IfModule>

Did you mean 你的意思是

RewriteRule ^(.*)$ $1/ [L]

instead? 代替?

I don't know about your approach. 我不知道你的做法。 I would try something like 我会尝试类似的东西

RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

I've used this for rewriting before and it works: 我以前用它来重写它的工作原理:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1
RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

Note that I didn't include the trailing slash in the first rule. 请注意,我没有在第一个规则中包含尾部斜杠。

Also, I would advice you not to rely on .htaccess unless absolutely necessary. 另外,除非绝对必要,否则我建议你不要依赖.htaccess。 This is because the server will check for .htaccess files constantly. 这是因为服务器会不断检查.htaccess文件。 Using the httpd.conf file instead means apache will only load conditions and rules once. 使用httpd.conf文件意味着apache只会加载条件和规则一次。 At least I was adviced to do so because of this. 至少我被建议这样做。

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

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