简体   繁体   English

删除.php并使用htaccess在URL中添加尾部斜杠而不加载CSS

[英]Remove .php and add trailing slash in url using htaccess not loading css

I have add following code to my htaccess file for removing .php extension and add trailing slash to the urls. 我已经将以下代码添加到我的htaccess文件中,以删除.php扩展名,并在网址中添加斜杠。 It worked fine but it reduced my page loading speed and my CSS code is not loading at all. 它工作正常,但降低了我的页面加载速度,而我的CSS代码根本没有加载。 All my pages look ugly. 我的所有页面看上去都很丑。

PLease help 请帮忙

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]

We can rewrite urls using .htaccess files. 我们可以使用.htaccess文件重写网址。 But before writing something in .htaccess files we need to verify which kind of server we are using. 但是,在.htaccess文件中写入内容之前,我们需要验证所使用的服务器类型。 You can easily check your server by just adding a 您只需添加一个即可轻松检查服务器

<?php phpinfo(); ?>

in a php page. 在php页面中。

Ensure that mod_rewrite module enabled in apache server. 确保在Apache服务器中启用了mod_rewrite模块。 This can be identified by checking phpinfo included page. 可以通过检查phpinfo包含的页面来识别。

Common methods used for url rewriting is as follows 网址重写的常用方法如下

RewriteEngine RewriteEngine叙述

This is mandatory for any apache server. 这对于任何apache服务器都是必需的。 This is used to enable the rewriting engine. 这用于启用重写引擎。 In some cases it should be on by default.So ensure that following code must be mandatory in top of your htaccess file 在某些情况下默认情况下它应该是打开的,因此请确保在htaccess文件顶部必须强制执行以下代码

RewriteEngine On

RewriteRule 重写规则

in some cases you may have only few pages and you can specify each page in your website in .htaccess file. 在某些情况下,您可能只有很少的页面,并且可以在.htaccess文件中指定网站中的每个页面。 In such case you can use these kind of rewriting For example 在这种情况下,您可以使用这种重写例如

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

Suppose you have to rewrite url like these 假设您必须像这样重写网址

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

You should write something like these on your .htaccess file 您应该在.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 http://en.wikipedia.org/w/index.php?title=url_rewriting

RewriteCond 的RewriteCond

This can be used for rewriting certain url with respect to some conditions. 这可以用于根据某些条件重写某些URL。 Suppose you need to add or remove www with your website name and on certain condition redirect to certain page like "url not available or error message " 假设您需要使用您的网站名称添加或删除www,并在某些情况下重定向到某些页面,例如“ URL不可用或错误消息”

Example : 范例:

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

You can further refer here 您可以在这里进一步参考

Introduction to url rewriting 网址重写简介

Rewrite with examples 用示例重写

These are the issues that you have for not loading css files 这些是您无法加载CSS文件的问题

Style sheets files may be treated without taking its extension and it wont load as css. 样式表文件可以不经扩展名处理,并且不会作为CSS加载。 You can do the following fixes on it 您可以对其进行以下修复

  • Use full path when including files in your html pages 在html页面中包含文件时使用完整路径

For example 例如

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

To

<link href="http://sitename.com/css/style.css" rel="stylesheet" type="text/css" />
  • Move the css to the project/public directory (like 将CSS移至project / public目录(例如

  • project/public/css) Use absolute paths to your css like /css/index.css. project / public / css),请使用/css/index.css等绝对路径。 Make sure the urls to your external files are not rewritten by the server. 确保服务器未重写外部文件的URL。

Another solution is try these method too 另一个解决方案是也尝试这些方法

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

Try This code: 试试下面的代码:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM