【问题标题】:Apache - Rewrite url without loopApache - 无循环重写 url
【发布时间】:2012-06-20 20:46:32
【问题描述】:

好吧,我再说一遍标题中所说的话,我无法找到一种方法来重写我所有的 url 而不会出现循环。 我尝试了很多选项,但找不到一种避免重定向循环的方法:

RewriteRule ^/(.+)  http://example.com/example/index.php$1  [R,L]                                                                                                                         
RewriteRule ^/?(.*) /var/www/example/index.php$1 [R]                                                                                                                              
RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/example/index.php$1 [R]  
RewriteRule ^$ /example/ [L]

下面是我的目录 apache-conf

<Directory /var/www/example>
  Options FollowSymLinks
  DirectoryIndex index.php
  Order allow,deny
  Allow from all
</Directory>

我理解为什么它会循环,尽管我无法想象(要么找到!)做我想做的事的好规则。

编辑

基本上,我将 OVH 托管的 example.fr 重定向到 IP 虚拟机。这会涉及到我的问题吗?

干杯

【问题讨论】:

    标签: mod-rewrite url-rewriting apache2


    【解决方案1】:
      RewriteEngine On
      RewriteCond %{REQUEST_URI} !^/example/
      RewriteRule (.*) /example/index.php/$1 [L,R]
    

    这会将 /example/ 之外的任何内容重定向到 /example/index.php,最后添加原始路径

    编辑: 因此,如果您希望将所有内容重定向到 /project_name/index.php,则需要将 RewriteCondRewriteRule 行中的单词“example”与您的项目名称交换...

    PS: 这里需要RewriteCond 行以确保重写不会循环(因此当您尝试此操作时,url 保持在/example/contact)。

    【讨论】:

    • 有了这个我仍然得到“它有效!”来自 apache 的页面。我已经重启了 apache /etc/init.d/apache2 restart
    • 1.你把这个放在哪里了? 2.AllowOverride指令的值是多少?
    • 在 /etc/apache2/httpd.conf 中。我从默认站点启用的文件 conf 中设置了 AllowOverride None。我让你知道情况如何。谢谢
    • 还是一样。我有“它有效!” / 上的页面,如果我转到 example.fr/example,那么我会访问主页。但是一旦我想去 example.fr/example/contact 我得到 404
    • 它是停留在/example.fr/example/contact URI 上还是当你得到 404 时 url 会改变?附:您想要 example.fr/example/contact 被准确重定向到哪里?到/example/index.php/example.fr/example/contact 或其他地方?
    【解决方案2】:

    此规则不应导致循环

    RewriteRule ^/(.+)  http://example.com/example/index.php$1  [R,L]
    

    这条规则根本不会做任何事情

    RewriteRule ^/?(.*) /var/www/example/index.php$1 [R]
    

    对于这个规则,你应该添加一个 RewriteCond

    RewriteCond %{REQUEST_URI} !^/example/
    RewriteRule ^/(.*)$ %{DOCUMENT_ROOT}/example/index.php$1 [R]
    

    这很好

    RewriteRule ^$ /example/ [L]
    

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多