简体   繁体   中英

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. It worked fine but it reduced my page loading speed and my CSS code is not loading at all. 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. But before writing something in .htaccess files we need to verify which kind of server we are using. You can easily check your server by just adding a

<?php phpinfo(); ?>

in a php page.

Ensure that mod_rewrite module enabled in apache server. This can be identified by checking phpinfo included page.

Common methods used for url rewriting is as follows

RewriteEngine

This is mandatory for any apache server. 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

RewriteEngine On

RewriteRule

in some cases you may have only few pages and you can specify each page in your website in .htaccess file. 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

You should write something like these on your .htaccess file

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

This can be used for rewriting certain url with respect to some conditions. 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 "

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

Style sheets files may be treated without taking its extension and it wont load as css. You can do the following fixes on it

  • Use full path when including files in your html pages

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

  • project/public/css) Use absolute paths to your css like /css/index.css. Make sure the urls to your external files are not rewritten by the server.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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