的.htaccess重写URL页或目录重写、目录、htaccess、URL

2023-09-02 11:42:34 作者:傷

有关我的地盘我有一个重写规则指向的网址 http://www.mysite.com/work到work.php文件。我也有称为目录工作,有它的文件,像project1.php,project2.php,等等...

For my site I have a RewriteRule that points the URL http://www.mysite.com/work to a work.php file. I also have a directory called "work" that has files in it, like project1.php, project2.php, etc...

什么规则,我会写这样的网址 http://www.mysite.com/work知道去到work.php文件,但URL http://www.mysite.com/工作/ PROJECT1 知道我的意思是里面去的目录工​​作并显示project1.php文件?

What rules would I have to write so that the URL http://www.mysite.com/work knows to go to the work.php file, but the URL http://www.mysite.com/work/project1 knows I mean to go inside the directory "work" and display the project1.php file?

修改:应该指出,这是我目前正在使用:

EDIT: Should point out, this is what I'm currently working with:

RewriteEngine On
RewriteBase /beta/
RewriteRule ^([a-z]+)$ $1.php [L]

任何额外的提示,以改善这种安全明智? (停止目录跳跃,等...)

Any additional tips to improve this security-wise? (Stopping directory jumping, etc...)

推荐答案

试试这个:

RewriteEngin On
RewriteBase /
RewriteRule ^work$ /work.php [QSA,L]

这将确保 http://www.mysite.com/work (没有尾随斜线)将转到 work.php 文件。

That will ensure that http://www.mysite.com/work (no trailing slash) will go to your work.php file.

如果您还想 http://www.mysite.com/work/ (带斜线)去 work.php 添加此行略高于去年重写规则

If you also want http://www.mysite.com/work/ (with trailing slash) to go work.php, add this line just above the last RewriteRule.

RewriteRule ^work/$ /work [R=301,QSA,L]

这会重定向到没有结尾的斜线的网址,因此,显示 work.php 文件。

That will redirect it to the URL with no trailing slash thus, displaying the work.php file.

更新:既然你已经有一个的RewriteBase 指令,只是把重写规则行之后(S)你的的RewriteBase 但你的前重写规则作为规则你使用一个包罗万象的和会匹配一切。

UPDATE: Since you already have a RewriteBase directive, just put the RewriteRule line(s) right after your RewriteBase but before your RewriteRule as the rule you're using a catch-all and will match everything.