重写适用于目录。但不适用于文件

发布于 2024-12-22 16:47:11 字数 1851 浏览 0 评论 0原文

我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2024-12-29 16:47:11

当然,您的重写规则应该仅在文件或目录存在时应用?

尝试使用这些行作为您的 RewriteCond

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Surely your rewrite rules should only be applied if a file or directory does not exist?

Try using these lines as your RewriteCond

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
猫弦 2024-12-29 16:47:11

在我的本地主机上尝试过,这工作正常:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ / [R=301,L]

RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
RewriteRule ^article/([^/\.]+)/?$ /index.php?page=article&name=$1 [L]

# For Future Reference, this works well for the final RewriteRule
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?page=$1&name=$2 [L]

作为替代解决方案,您还可以将所有网址重写为index.php:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ / [R=301,L]

RewriteRule ^(.*)$ / [L]

第一个RewriteRule实际上会重定向浏览器,但第二个只会翻译它。这样做的好处是,index.php 将收到原始 url 的 REQUEST_URI:

$parts = explode('/', $_SERVER['REQUEST_URI']); // array( 0 => 'article', 1 => 'iphone', 2 => '')

Tried it on my localhost, and this worked properly:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ / [R=301,L]

RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
RewriteRule ^article/([^/\.]+)/?$ /index.php?page=article&name=$1 [L]

# For Future Reference, this works well for the final RewriteRule
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?page=$1&name=$2 [L]

As an alternate solution, you could also rewrite all the urls to index.php:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ / [R=301,L]

RewriteRule ^(.*)$ / [L]

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:

$parts = explode('/', $_SERVER['REQUEST_URI']); // array( 0 => 'article', 1 => 'iphone', 2 => '')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文