简体   繁体   中英

htaccess url forwarding to php file with and without trailing slash

I've been looking for something like this (that works of course) for YEARS now. Would be great if someone could finally produce this code.

At the moment, I've been working with this:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)\/$ $1.php [NC]

And it works great but doesn't work without a trailing slash. And the annoying this is it treats all included files a folder forward so I am forever adding ../ to all css and js files.

Apart from the above, it works like this (as intended):

  1. if www is not present, this will get added
  2. http://www.example.com/page.php will go to page.php
  3. http://www.example.com/page/ will go to page.php
  4. http://www.example.com/page will come up with a 404 error as it's looking for the folder

I've tried loads of different things by Googling it but have had no success. All I need it to do is to like the url without the trailing slash, whether it forwards it to a trailing slash that way or whether it likes both. The whole idea is for it to read a .php file and display it in the window as a nice SEO type link: www.example.com/page

Options +FollowSymlinks
RewriteEngine On
DirectorySlash Off
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [NC]

<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This works. Answers this question. Can anyone improve this?

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