【问题标题】:.htaccess file change url string not working on some pages.htaccess 文件更改 url 字符串在某些页面上不起作用
【发布时间】:2014-09-02 22:53:33
【问题描述】:

我已更新我的 .htaccess 文件以从 url 字符串中删除 .php 扩展名。代码在这里:

ErrorDocument 404 /lost.php

RewriteEngine On
RewriteBase /

# remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

一切正常,并且正在删除 .php。唯一的问题是,当我尝试访问网站的某个区域(客户区)时,页面根本无法正常显示。截图如下:

在将上述代码添加到 .htaccess 文件之前,url 为:

www.mysite.co.uk/client-area.php

将上述代码添加到.htaccess文件后,url为:

www.mysite.co.uk/client-area/

页面没有正确显示?知道为什么会发生这种情况以及需要对 .htaccess 文件代码做些什么来解决这个问题吗?欢迎任何帮助:)

【问题讨论】:

    标签: php .htaccess url


    【解决方案1】:

    保持您的 .htaccess 代码如下:

    ErrorDocument 404 /lost.php    
    RewriteEngine On
    RewriteBase /
    
    # remove enter code here.php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    # remove index
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteRule (.*)/index$ $1/ [R=301,L,NE]
    
    # remove slash if not directory
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/$ $1 [R=301,L,NE]
    
    # add .php to access file, but don't redirect
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)/?$ /$1.php [L]
    

    要显示 css/js/images,只需在 css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 / 开头。

    或者您可以尝试将其添加到页面的 HTML 标头中:<base href="/" />,以便从该 URL 而不是当前 URL 解析每个相对 URL。

    【讨论】:

    • 我已将每个网址更新为绝对网址,并将您的代码上传到 .htaccess 文件。它工作得很好,比以前的代码更好。唯一的问题是,当我输入登录详细信息时,什么也没有发生。同样在联系页面上,如果提交的表单没有数据,它应该会出现错误,但这也不起作用。我认为这两个问题都是一旦满足条件,就会调用 .php 页面。有没有办法解决这个问题?嘿,感谢您的帮助。
    • 我认为表单正在使用 POST 方法。立即尝试添加更新的规则。
    • 完美运行!你是个天才,非常感谢你的帮助:)
    猜你喜欢
    • 2012-04-13
    • 1970-01-01
    • 2013-04-03
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多