【问题标题】:htaccess for Dynamic URL Rewrite with .html extension based on url selectionhtaccess 用于基于 url 选择的带有 .html 扩展名的动态 URL 重写
【发布时间】:2013-08-19 08:53:18
【问题描述】:

我正在尝试重写一些动态页面 url,其中数据是从数据库中获取的,例如“this-is-a-link”。

        Options +FollowSymlinks
        RewriteEngine On

        RewriteBase /testweb/

        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^(.*)$ faq_pages.php?page=$1 [QSA,L]

http://www.test.com/testweb/this-is-a-link 可以正常工作

但我需要根据点击的任何链接更改重写规则,如下所示。

http://www.test.com/testweb/faq/this-is-a-link, 'testweb/news/this-is-another-link','/testweb/testimonials/test-link'

上述 RewriteRule 中的“faq_pages.php”应改为“news_pages.php”。我怎样才能动态拥有它,以便我可以使用具有相同 htaccess 的每个页面,或者如何检查并将值重写到 htaccess 中的不同页面。

另外请告诉我如何更正下面的代码。

        Options +FollowSymlinks
        RewriteEngine on
        RewriteRule ^(.*)\.html$ faq_pages.php?page=$1

我需要在地址栏中显示 html 网址,例如“/testweb/this-is-a-link.html”或“/testweb/faq/this-is-a-link.html”

编辑:在 Jon Lin 的帮助下,我更新了如下代码。

        Options +FollowSymlinks
        RewriteEngine On

        RewriteBase /testweb/

        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^faq/(.*?)/?$ faq_pages.php?page=$1 [QSA]

现在以下两种情况都有效: /testweb/faq/another-faq-test.html,
/testweb/faq/another-faq-test.html/

由于我没有任何名为 'faq' 的子文件夹,根文件夹中的模板文件(images/css/js 等)没有加载。 我该如何解决?

谢谢你

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting dynamic-url htmlextensions


    【解决方案1】:

    你在寻找这样的东西吗?

        Options +FollowSymlinks
        RewriteEngine On
    
        RewriteBase /testweb/
    
        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^faq/(.*)$ faq_pages.php?page=$1 [QSA,L]
    
        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^news/(.*)$ news_pages.php?page=$1 [QSA,L]
    

    【讨论】:

    • 您好,感谢您的回答。我使用 RewriteRule ^faq/(.*)/$ faq_pages.php?id=$1 [QSA] 和 RewriteRule ^faq/(.*)$ faq_pages.php?id=$1 [QSA] 重写 testweb/faq/another -faq-test.html/ 和 testweb/faq/another-faq-test.html 。请告诉我这是否是正确的方法。另外请帮助我忽略/重写“faq”文件夹,因为当前 css 或图像未加载,因为 url 中有一个“faq/”。非常感谢
    • @user2669827 而不是使用 2 个规则,只需在 / 之后添加一个 ?。如果您在/faq/ 中请求静态文件,则 2 个重写条件会阻止应用规则
    • 嗨,再次感谢。 RewriteRule ^faq/(.*)/?$ faq_pages.php?id=$1 [QSA] 不适用于 /testweb/faq/another-faq-test.html/ 。我只使用'faq/'和'news/'来检查url并重写到不同的页面。我没有任何带有这些名称的文件夹。我怎样才能忽略它或指定它们不是目录.. 现在 url 查找文件夹'faq'
    • @user2669827 使用正则表达式:^faq/(.*?)/?$。并在你的问题或其他东西中格式化代码,在 cmets 中是不可能读取的
    • 太棒了.. 现在 html/ url 也可以工作了。我将编辑问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多