的.htaccess重写URL页或目录 [英] .htaccess rewriting url to page or directory

查看:117
本文介绍了的.htaccess重写URL页或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的地盘我有一个重写规则指向的网址 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.

这篇关于的.htaccess重写URL页或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆