简体   繁体   English

htaccess网址转发到php文件(带斜杠和不带斜杠)

[英]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. 我一直在为YEARS寻找这样的东西(当然有效)。 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. 令人讨厌的是,它将所有包含的文件都向前处理一个文件夹,所以我永远将../添加到所有css和js文件中。

Apart from the above, it works like this (as intended): 除上述内容外,它的工作方式如下(预期):

  1. if www is not present, this will get added 如果不存在www,则将添加
  2. http://www.example.com/page.php will go to page.php http://www.example.com/page.php将转到page.php
  3. http://www.example.com/page/ will go to page.php http://www.example.com/page/将转到page.php
  4. http://www.example.com/page will come up with a 404 error as it's looking for the folder http://www.example.com/page在寻找资料夹时会出现404错误

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. 我需要做的就是喜欢没有结尾斜杠的url,无论是以这种方式将其转发到结尾斜杠还是都喜欢。 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 整个想法是要读取一个.php文件并将其显示在窗口中,作为一个不错的SEO类型链接: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? 有人可以改善吗?

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

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