[英]How to name file and directory the same with .htaccess rewrite rules?
I am trying to work on my "pretty-urls" and I'll be the first to admit I really don't know what I'm doing. 我正在尝试处理我的“漂亮网址”,我将是第一个承认我真的不知道自己在做什么的人。 I'm trying to wrap my head around it though.
我正尽力将它缠住。 So what I am trying to do is have a file.php, but then also have a directory called file.
所以我想做的是有一个file.php,然后还有一个名为file的目录。 That way I can go to www.example.com/file and it should pull up file.php.
这样,我可以转到www.example.com/file,它应该可以拉起file.php。 But I also want to be able to go to www.example.com/file/subfile and it should go to subfile.php IN the file directory.
但我也希望能够转到www.example.com/file/subfile,它应该转到文件目录中的subfile.php。
Right now, I'm able to get rid of ".php" on all files and I've figured out how to "hide" the $_GET parameters in the URL, but this directory/file issue is throwing me for a loop. 现在,我可以摆脱所有文件中的“ .php”,而且我已经找到了如何“隐藏” URL中的$ _GET参数,但是这个目录/文件问题使我陷入了循环。 Any help is appreciated!
任何帮助表示赞赏!
This is my current .htaccess 这是我目前的.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dire1/file/([0-9]+) dir1/file.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dir2/example/([0-9]+) dir2/example.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
This is because of mod-dir
Apache directory module that runs before mod-rewrite
. 这是因为在
mod-rewrite
之前运行的mod-dir
Apache目录模块。 When you request an existent directory without a traling slash the mod-dir
issues a permanent 301 Redirect from /dir
to /dir/
and that is why you are unable to rewrite the uri. 当您请求不带斜杠的现有目录时,
mod-dir
从/dir
到/dir/
发出永久性301重定向,这就是为什么您无法重写uri的原因。
To fix this, you need to tell mod-dir not to perform the redirection when a directory without a traling slash is requested. 要解决此问题,您需要在请求目录中没有反斜杠的目录时告诉mod-dir不要执行重定向。 You can use
DirectorySlash
directive in your htaccess to change this behaviour. 您可以在htaccess中使用
DirectorySlash
指令来更改此行为。
Add the following line to your htaccess : 将以下行添加到您的htaccess中:
DirectorySlash off
Reference : https://httpd.apache.org/docs/2.4/mod/mod_dir.html 参考: https : //httpd.apache.org/docs/2.4/mod/mod_dir.html
Have it this way: 有这种方式:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dire1/file/([0-9]+) dir1/file.php?id=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^dir2/example/([0-9]+) dir2/example.php?id=$1 [NC,QSA,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Last rule will serve /file.php
when URI is example.com/file/
当URI为
example.com/file/
时,最后一条规则将投放/file.php
。
Note that if you serve example.com/file
(no trailing slash) then Apache's mod_dir
module will add a trailing slash in the end to make it example.com/file/
and then server /file.php
. 请注意,如果您提供
example.com/file
(不带斜杠),则Apache的mod_dir
模块将在末尾添加一个斜杠,使其成为example.com/file/
,然后是服务器/file.php
。
Turning off this trailing slash behavior is considered a security risk and it may expose your internal directory structure to your visitors. 关闭此尾部的斜杠行为被视为安全隐患,并且可能会将内部目录结构暴露给访问者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.