重写适用于目录。但不适用于文件
我的代码:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
这有效。但如果我提供文件名或目录,它就会失败。
我想要的:
- 如果用户请求目录或文件名,则重定向到主页。
否则,应该评估此规则:
RewriteRule ^article/([az]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([az]+)/?$ /index.php?page=$1 [L]
我的网站根目录下有一个 index.php 文件:“http://mysite.com /index.php”
示例请求:
User typed url --- Physical matched url --- Comments
----------------------------------------------------------
http://mysite.com/cinema --- http://mysite.com/index.php?page=cinema --- display cinema page
http://mysite.com/contacus --- http://mysite.com/index.php?page=contactus --- display contact us page
http://mysite.com/article/iphone --- http://mysite.com/index.php?page=article&name=iphone --- display article
http://mysite.com/images --- http://mysite.com/index.php --- redirect to index page, because it is a physical directory
http://mysite.com/js --- http://mysite.com/index.php --- redirect to index page, because it is a physical directory
我的部分工作代码:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ / [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
如果我使用上面的代码,它部分工作。也就是说,
http://mysite.com/images --- http://mysite.com/?page=images is displayed in address bar
http://mysite.com/images/ --- http://mysite.com/ is displayed in address bar
当我使用 RewriteCond %{REQUEST_FILENAME} -f
作为第二个条件(在第一个条件行之后)时,它将不起作用。
请帮我
My code:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
This works. But if I supply a filename or directory, it fails.
What i want:
- redirect to home page, if user requests a directory or filename.
else, this rule should be evaluated:
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
There is one index.php file at root of my site: "http://mysite.com/index.php"
Sample requests:
User typed url --- Physical matched url --- Comments
----------------------------------------------------------
http://mysite.com/cinema --- http://mysite.com/index.php?page=cinema --- display cinema page
http://mysite.com/contacus --- http://mysite.com/index.php?page=contactus --- display contact us page
http://mysite.com/article/iphone --- http://mysite.com/index.php?page=article&name=iphone --- display article
http://mysite.com/images --- http://mysite.com/index.php --- redirect to index page, because it is a physical directory
http://mysite.com/js --- http://mysite.com/index.php --- redirect to index page, because it is a physical directory
My partially working code:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ / [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
If i use above code, it works partially. That is,
http://mysite.com/images --- http://mysite.com/?page=images is displayed in address bar
http://mysite.com/images/ --- http://mysite.com/ is displayed in address bar
When I use RewriteCond %{REQUEST_FILENAME} -f
as second condition (after first condition's line), it will not work.
Please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,您的重写规则应该仅在文件或目录不存在时应用?
尝试使用这些行作为您的
RewriteCond
Surely your rewrite rules should only be applied if a file or directory does not exist?
Try using these lines as your
RewriteCond
在我的本地主机上尝试过,这工作正常:
作为替代解决方案,您还可以将所有网址重写为index.php:
第一个RewriteRule实际上会重定向浏览器,但第二个只会翻译它。这样做的好处是,index.php 将收到原始 url 的 REQUEST_URI:
Tried it on my localhost, and this worked properly:
As an alternate solution, you could also rewrite all the urls to index.php:
The first RewriteRule will actually redirect the browser, but the second one will just translate it. The upside to this is that index.php will receive the REQUEST_URI of the original url: