【问题标题】:mod_rewrite not working htaccessmod_rewrite 不工作 htaccess
【发布时间】:2017-06-12 19:23:06
【问题描述】:

我有一个带有 11 个子域的 website。我在 public_html 根文件夹中有一个主 htaccess 文件,它具有 cache 等常见的共享特征。我在其中有单独的 htaccess 文件我使用mod_rewrite 的每个子域根文件夹但是我无法让它重写 URL。我正在尝试将其从dundaah.com/days/content.php?day=thur 更改为更干净的dundaah.com/thursday,并将子域music.dundaah.com/days/content.php?day=thur 更改为首选music.dundaah.com/thursday。到目前为止,我的第一个研究代码只能让我到达 URL dundaah.com/day/thur,而第二个到 dundaah.com/thursday 仍然给我一个 404 错误。我没有更改网站的内部链接,这是一个问题吗?有人可以帮忙吗?谢谢

<IfModule mod_rewrite.c>

  Options +FollowSymlinks
  
  # enable the rewrite engine
  RewriteEngine On
  RewriteCond %{REQUEST_URI} ^/days/content.php
  RewriteCond %{QUERY_STRING} ^day=([a-z]+)$
  RewriteRule ^(.*)$ /days/%1? [R,L]

  # or for individual pages 
  RewriteCond %{REQUEST_URI} ^/days/content.php
  RewriteCond %{QUERY_STRING} ^day=thur$
  RewriteRule ^(.*)$ /thursday? [R,L]
</IfModule>

【问题讨论】:

  • 如果您直接在浏览器地址栏中输入 url example.com/thursday 是否有效?
  • no not while using the above code
  • 每一天都需要不同的规则或者你可以使用RewriteMap

标签: php apache .htaccess mod-rewrite


【解决方案1】:

抱歉,我是那个在我的网站上使用了错误的文件夹目录系统的人。这是正在使用的那个

public_html(www directory) -index.php -vidz(sub-domain) -index.php -days -_imports -css -js -php -content.php -contact.php

这是我的升级系统

public_html(www directory) -index.php -vidz(sub-domain) -index.php -content.php -contact.php -assets -_imports -css -js -php

这使得链接成为&lt;a href='monday'&gt;Mon&lt;/a&gt; 并使用下面的.htaccess 重定向规则成为可能。

<IfModule mod_rewrite.c>

    Options +FollowSymlinks
    RewriteEngine On   
    RewriteRule ^monday$ content.php?day=mon [NC,L]
    RewriteRule ^tuesday$ content.php?day=tue [NC,L]
    RewriteRule ^wednesday$ content.php?day=wed [NC,L]
    RewriteRule ^thursday$ content.php?day=thur [NC,L]
    RewriteRule ^friday$ content.php?day=fri [NC,L]
    RewriteRule ^saturday$ content.php?day=sat [NC,L]
    RewriteRule ^sunday$ content.php?day=sun [NC,L]
    RewriteRule ^contact$ contact.php [NC,L]

</IfModule>

感谢所有回答的人!它帮助我意识到了这一点。

【讨论】:

    【解决方案2】:

    这将使/thursday 工作(以及所有其他日子),将其放入您的主.htaccess 和您的子域中。它还会将您的旧网址重定向到新网址。

    仅当您使用 Apache 2.4 或更高版本时才有效。如果您不是,请告诉我,我会更新答案。

    RewriteEngine on
    
    # Redirect old URLs
    RewriteCond %{QUERY_STRING} =day=mon
    RewriteRule ^days/content\.php$ /monday [R=301,END]
    RewriteCond %{QUERY_STRING} =day=tue
    RewriteRule ^days/content\.php$ /tuesday [R=301,END]
    RewriteCond %{QUERY_STRING} =day=wed
    RewriteRule ^days/content\.php$ /wednesday [R=301,END]
    RewriteCond %{QUERY_STRING} =day=thur
    RewriteRule ^days/content\.php$ /thursday [R=301,END]
    RewriteCond %{QUERY_STRING} =day=fri
    RewriteRule ^days/content\.php$ /friday [R=301,END]
    RewriteCond %{QUERY_STRING} =day=sat
    RewriteRule ^days/content\.php$ /saturday [R=301,END]
    RewriteCond %{QUERY_STRING} =day=sun
    RewriteRule ^days/content\.php$ /sunday [R=301,END]
    
    # Rewrite new URLs
    RewriteRule ^monday$ days/content.php?day=mon [END]
    RewriteRule ^tuesday$ days/content.php?day=tue [END]
    RewriteRule ^wednesday$ days/content.php?day=wed [END]
    RewriteRule ^thursday$ days/content.php?day=thur [END]
    RewriteRule ^friday$ days/content.php?day=fri [END]
    RewriteRule ^saturday$ days/content.php?day=sat [END]
    RewriteRule ^sunday$ days/content.php?day=sun [END]
    

    最好单独处理这些日子,因为脚本使用简短格式而新 URL 不使用。如果需要,您可以更改它们,或者如果我遗漏了什么,请告诉我。

    【讨论】:

      【解决方案3】:

      Apachenginx 都具有内部外部重定向的概念。 p>

      您可以做的是创建一个“重定向循环”,其中/days/content.php?day=thurredirect/R(外部重定向)到/thursday,而/thursday 将在内部返回到@ 987654329@,最初可能看起来像一个循环。

      见:

      这可以通过在每种情况下针对 request uri 进行测试来完成:

      RewriteCond %{REQUEST_URI} ^/days/content.php$
      RewriteCond %{QUERY_STRING} ^day=thur$
      RewriteRule ^(.*)$ /thursday? [R,L] # external redirect
      
      RewriteCond %{REQUEST_URI} ^/thursday$
      RewriteRule ^(.*)$ /days/content.php?day=thur [L] # internal redirect
      

      【讨论】:

      • 这很好,浏览器中的 url 显示 dundaah.com/thursday 但不显示内容只是 dundaah.com redirected you too many times 并且我应该尝试清除我的 cookie 但即使我这样做了,仍然收到相同的消息.内部链接仍然保留days/content.php?day=thur 用于索引页面,content.php?day=thur 用于days 文件夹内的文件,它们只是两个content.php &amp; contact.php。我没有任何删除 .php 扩展的代码。
      • @MaxNjoroge,你确定只有一个R吗?听起来您正在通过在每种情况下都有外部重定向来创建一个实际的循环,而不是在另一种情况下使用内部重定向。
      • 您不能使用“REQUEST_URI”,因为它是通过重写更新的。您可以使用的是“THE_REQUEST”,它是原始请求并且不会被修改。或者只是像我在回答中那样使用 [END],因此 mod_rewrite 在成功匹配后不再进行任何处理。
      猜你喜欢
      • 2010-10-31
      • 2011-07-31
      • 2015-12-02
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2011-04-14
      相关资源
      最近更新 更多