【问题标题】:htaccess is not redirecting me on index.php page or default pagehtaccess 没有在 index.php 页面或默认页面上重定向我
【发布时间】:2017-02-28 15:21:51
【问题描述】:

当我尝试继续 http://localhost/themobilesapp 时,它仍然会在 specification.php 页面上重定向我,而不是在 index.php 上重定向我

现在,我将通过键入 http://localhost/themobilesapp/index.php

进入我的主页

这是我的完整 htaccess 代码。

Options +FollowSymLinks -MultiViews -Indexes

<IfModule mod_rewrite.c>

DirectoryIndex index.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

ErrorDocument 404 404error.php

RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]

RewriteRule ^news/([A-Za-z0-9-]+)?$ news.php?url=$1 [L]

</IfModule>

如何通过键入 http://localhost/themobilesapp 将其重定向到 index.php 上?

【问题讨论】:

    标签: php .htaccess redirect


    【解决方案1】:

    试试下面这样的规则,

    DirectoryIndex index.php
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    ErrorDocument 404 404error.php
    
    RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
    
    RewriteRule ^news/([A-Za-z0-9-]+)?$ news.php?url=$1 [L]
    
    RewriteRule ^$ /themobilesapp/index.php [R=301,L]
    

    【讨论】:

    【解决方案2】:

    我在你的 htaccess 中没有看到 index.php

    我想你的意思是这样的:

    RewriteRule ^(.*)$ index.php [L]
    

    这意味着在此重写之下的其他重定向将不会被执行。订单事项;在需要时使用 L 标志。

    你的正则表达式:

    RewriteRule ^([A-Za-z0-9-]+)?$ specification.php?url=$1 [L]
    

    匹配所有内容(甚至是空字符串;如您的示例所示)。如果离开最后?出去;另一个字符串不是可选的:

    RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L]
    

    【讨论】:

    • 它正在重定向索引页面上的 localhost/themobilesapp。主页上有很多页面链接,然后它也将其重定向到 index.php
    • 然后删除RewriteRule ^(.*)$ index.php [L]这一行;并保留修改后的规范行。
    • 我没有添加任何东西。我只从 RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L] 中删除 ? 然后我的代码重定向 index.php
    猜你喜欢
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2012-09-11
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多