【问题标题】:Htaccess Remove Index.php From A URLHtaccess 从 URL 中删除 Index.php
【发布时间】:2017-03-15 03:05:19
【问题描述】:

我看过这个:htaccess remove index.php from url,但它不起作用,并且在 cmets 中似乎对最佳答案存在分歧。

如果我输入 http://www.example.com/thanksgiving/index.php/getThankyou 我会得到我想要的页面。

如果我输入 http://www.example.com/thanksgiving/getThankyou 我会得到 404。

我在 htaccess 中尝试过的内容:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/thanksgiving/(.*)/$ /thanksgiving/index.php/$1 [L]
</IfModule>

我该怎么做才能使/thanksgiving/getThankyou/ 或任何以/thanksgiving 开头的网址在网址中导致正确的资源被提供?同样,当index.php 在 url 中时,它当前可用。

我也尝试过:RewriteRule ^/thanksgiving/[^/]*/$ /thanksgiving/index.php/$1 [L],但这也没有用...

编辑:似乎 htaccess 的一部分是由 wordpress 设置的,并且可能与上述期望的行为冲突:

同一个htaccess的wordpress部分是:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

这是干扰吗?如何将其与不属于 Wordpress 应用程序的 /thanksgiving/ url 进行协调?

编辑:

以上只针对root (public_html) htaccess,下面是/thanksgiving/本身的htaccess,请分析冲突,谢谢大家的帮助!

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteBase /
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
# Do not change this line. 
# Change example.com to your domain name
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 

# Change your_app_name to the subfolder name
RewriteCond %{REQUEST_URI} !^/thanksgiving/ 

# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Change your_app_name to the subfolder name
# Change example.com to your domain name
RewriteRule ^(.*)$ /thanksgiving/$1 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ thanksgiving/index.php [L]

【问题讨论】:

  • 指向另一个答案的链接是正确的。我不确定你为什么改变了RewriteRule 的方式。将其改回RewriteRule ^(.*)$ /index.php?/$1 [L]。它也不需要在IfModule 之间。应该只使用RewriteEngine On
  • @thickguru 我改变了它,因为我想重写的唯一网址是那些以/thanksgiving/开头的网址

标签: wordpress .htaccess laravel


【解决方案1】:

您需要从重写模式中删除 前导斜杠

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:thanksgiving/)?(.*)$ /thanksgiving/index.php/$1 [L]
</IfModule>

否则规则将接受以双斜杠开头的 uri(即://thanksgiving/)。

【讨论】:

  • 不行,第二个前导斜杠也需要去掉吗?
  • @SummerDeveloper 在你的 wp 或其他 rewriteRules 之前添加这个。
  • 仍然是 404。你能仔细检查你的RewriteRule 行吗?在我看来它仍然有点偏离,但我无法确定为什么......
  • @SummerDeveloper 你是对的。有一个轻微的错字。我已经删除了尾部的斜杠。
  • 非常感谢。你真棒!你真的应该在你的个人资料上设置一个捐赠链接,你的耐心和智慧需要得到回报!
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2021-10-02
  • 2015-01-20
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
相关资源
最近更新 更多