如何使用.htaccess重写规则命名文件和目录? [英] How to name file and directory the same with .htaccess rewrite rules?
问题描述
我正在尝试处理"pretty-urls",并且我将是第一个承认我真的不知道自己在做什么的人.我正尽力将它缠住.所以我想做的是拥有一个file.php,但是还有一个名为file的目录.这样,我可以转到www.example.com/file,它应该可以拉起file.php.但我也希望能够转到www.example.com/file/subfile,它应该转到文件目录中的subfile.php.
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. That way I can go to www.example.com/file and it should pull up 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.
现在,我可以删除所有文件上的".php",而且我已经找到了如何隐藏" URL中的$ _GET参数,但是此目录/文件问题使我无法使用一个循环.感谢您的帮助!
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. Any help is appreciated!
这是我当前的.htaccess
This is my current .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]
推荐答案
具有这种方式:
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]
当URI为example.com/file/
请注意,如果您提供example.com/file
(不带斜杠),则Apache的mod_dir
模块将在末尾添加一个斜杠以使其成为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
.
关闭这种尾随的斜杠行为被视为安全隐患,并且可能会将内部目录结构暴露给访问者.
Turning off this trailing slash behavior is considered a security risk and it may expose your internal directory structure to your visitors.
这篇关于如何使用.htaccess重写规则命名文件和目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!