【问题标题】:htaccess how to don't rewrite certain URLhtaccess 如何不重写某些 URL
【发布时间】:2018-03-28 02:53:21
【问题描述】:

各位开发者您好。

我正在编写一个混合网站,其中包含诸如 index.php、page1.php... 之类的静态页面和一个安装在与静态网站相同级别的顶级目录中的 Nibbleblog。

为了在博客中启用对 SEO 友好的 URL,我在 htaccess 中添加了 Nibbleblog 推荐的以下重写规则。

RewriteBase /eurotint/new-euroting/app/ 是我当前的本地开发目录

RewriteBase /eurotint/new-euroting/app/
RewriteRule ^category/([^/]+)/page-([0-9]+)$ blog.php?controller=blog&action=view&category=$1&number=$2 [L]
RewriteRule ^category/([^/]+)/$ blog.php?controller=blog&action=view&category=$1&number=0 [L]
RewriteRule ^tag/([^/]+)/page-([0-9]+)$ blog.php?controller=blog&action=view&tag=$1&number=$2 [L]
RewriteRule ^tag/([^/]+)/$ blog.php?controller=blog&action=view&tag=$1&number=0 [L]
RewriteRule ^page-([0-9]+)$ blog.php?controller=blog&action=view&number=$1 [L]
RewriteRule ^post/([^/]+)/$ blog.php?controller=post&action=view&post=$1 [L]
RewriteRule ^post-([0-9]+)/(.*)$ blog.php?controller=post&action=view&id_post=$1 [L]
RewriteRule ^page/([^/]+)/$ blog.php?controller=page&action=view&page=$1 [L]
RewriteRule ^feed/$ feed.php [L]
RewriteRule ^([^/]+)/$ blog.php?controller=page&action=$1 [L]

现在的问题:

这对于在博客中启用 SEO 友好的 URL 非常有效,但是,如果我输入了带有地址的博客文章,即 websiteAddress/eurotint/new-euroting/app/post/welcome-post/

然后,每当我在此或其他博客页面(即类别等)上单击其中一个静态页面地址(索引、page1、page2)时。

重写规则将地址重定向到类似于: websiteAddress/eurotint/new-euroting/app/post/welcome-post/index.php

但我不希望重写静态地址,即单击主页按钮应将我带到索引页面 websiteAddress/eurotint/new-euroting/app/index.php 而不是 websiteAddress/eurotint/new-euroting/app/post/welcome-post/index.php

我已经为我不想重定向到博客页面的文件名尝试了一些 301 重定向,但它们都没有工作,而且 tbh。我不是 Apache 重写规则方面的专家。

此外,在博客文章/类别中,静态站点的所有图像 url 等也在被重写,我也想停止这种情况,只重写与博客相关的 url .

任何有关如何实现这一目标的帮助将不胜感激,谢谢。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    可能最好的处理方法是使用 filedirectory 条件 - 但因为 RewriteCond 只会影响 next 规则,你要么需要在每个规则之前添加这些条件,或者创建一个跳过规则:

    RewriteBase /eurotint/new-euroting/app/
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [S=10]
    RewriteRule ^category/([^/]+)/page-([0-9]+)$ blog.php?controller=blog&action=view&category=$1&number=$2 [L]
    RewriteRule ^category/([^/]+)/$ blog.php?controller=blog&action=view&category=$1&number=0 [L]
    RewriteRule ^tag/([^/]+)/page-([0-9]+)$ blog.php?controller=blog&action=view&tag=$1&number=$2 [L]
    RewriteRule ^tag/([^/]+)/$ blog.php?controller=blog&action=view&tag=$1&number=0 [L]
    RewriteRule ^page-([0-9]+)$ blog.php?controller=blog&action=view&number=$1 [L]
    RewriteRule ^post/([^/]+)/$ blog.php?controller=post&action=view&post=$1 [L]
    RewriteRule ^post-([0-9]+)/(.*)$ blog.php?controller=post&action=view&id_post=$1 [L]
    RewriteRule ^page/([^/]+)/$ blog.php?controller=page&action=view&page=$1 [L]
    RewriteRule ^feed/$ feed.php [L]
    RewriteRule ^([^/]+)/$ blog.php?controller=page&action=$1 [L]
    

    条件 (RewriteCond) 表明如果请求的文件名 一个文件 一个目录 - skip the next 10 rules,那么它应该 跳过所有重写规则。

    这假设您定义的重写匹配都没有指向实际存在的文件/目录(例如,您实际上没有名为 feed 的目录)。

    【讨论】:

    • 您好,谢谢您的回答,我已经尝试了您的建议,但我无法让它发挥作用。我最终在导航栏中使用了完整的网址来解决这个问题,即我使用了 https : // www。网址。 com/ filreUrl,我知道它并不理想,但它解决了这个特定问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多