【问题标题】:Remove .php and add trailing slash in url using htaccess not loading css使用 htaccess 删除 .php 并在 url 中添加斜杠而不加载 css
【发布时间】:2014-04-09 06:05:58
【问题描述】:

我已将以下代码添加到我的 htaccess 文件中,以删除 .php 扩展名并在 URL 中添加尾部斜杠。它工作得很好,但它降低了我的页面加载速度,我的 CSS 代码根本没有加载。我所有的页面看起来都很丑。

请帮忙

RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]

# add a trailing slash    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    我们可以使用 .htaccess 文件重写 url。但在 .htaccess 文件中写入内容之前,我们需要验证我们使用的是哪种服务器。您只需添加一个

    即可轻松检查您的服务器
    <?php phpinfo(); ?>
    

    在一个 php 页面中。

    确保在 apache 服务器中启用了 mod_rewrite 模块。这可以通过检查 phpinfo 包含的页面来识别。

    url重写常用方法如下

    重写引擎

    这对于任何 apache 服务器都是必需的。这用于启用重写引擎。在某些情况下,它应该是默认开启的。因此,请确保在您的 htaccess 文件顶部必须强制执行以下代码

    RewriteEngine On
    

    重写规则

    在某些情况下,您可能只有几个页面,您可以在 .htaccess 文件中指定网站中的每个页面。在这种情况下,您可以使用这些重写例如

    RewriteRule ^article/used/to/be/here.php$ /article/now/lives/here/ [R=301,L]
    

    假设你必须像这样重写 url

    http://en.wikipedia.org/wiki/url_rewriting

    你应该在你的 .htaccess 文件上写一些类似的东西

    RewriteEngine On
    RewriteRule   ^wiki/(.+)$   w/index.php?title=$1   [L]
    But actual implementation of the page will be like these
    

    http://en.wikipedia.org/w/index.php?title=url_rewriting

    RewriteCond

    这可用于针对某些条件重写某些 url。假设您需要使用您的网站名称添加或删除 www,并在某些情况下重定向到某个页面,例如“url 不可用或错误消息”

    例子:

    RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC] 
    

    您可以进一步参考这里

    Introduction to url rewriting

    Rewrite with examples

    这些是你没有加载 css 文件的问题

    样式表文件可以在不带扩展名的情况下进行处理,并且不会作为 css 加载。您可以对其进行以下修复

    • 在 html 页面中包含文件时使用完整路径

    例如

    <link href="css/style.css" rel="stylesheet" type="text/css" />
    

    <link href="http://sitename.com/css/style.css" rel="stylesheet" type="text/css" />
    
    • 将css移动到project/public目录下(如

    • project/public/css) 使用您的 css 的绝对路径,例如 /css/index.css。确保外部文件的 url 没有被服务器重写。

    另一种解决方法是也试试这些方法

    RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
    

    【讨论】:

    • 先生,这不是我的问题。我只是问 htaccess 工作正常,但是在添加上面的 htaccess 代码后,我的 css 文件无法正常工作
    • @Alex : 我在底部添加了答案请尝试这些
    【解决方案2】:

    试试这个代码:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/$ $1.php
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多