简体   繁体   English

如何使用htaccess从漂亮的URL中忽略文件夹

[英]How to ignore a folder from beautiful URL using htaccess

I am developing a website, assuming example.com and a blog located at example.com/blog. 我正在开发一个网站(假设example.com)和位于example.com/blog的博客。 The website language is PHP (I designed and programmed it by myself), and I used wordpress as the blog. 网站语言是PHP(我自己设计和编程),并且我使用wordpress作为博客。

I tried to use beautiful URL in main part of the website to convert example.com/services.php to example.com/services by means of this code in htaccess: 我试图通过htaccess中的以下代码在网站的主要部分中使用漂亮的URL将example.com/services.php转换为example.com/services:

# BEAUTIFUL URL
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]

The main part of the website works fine. 该网站的主要部分工作正常。 But when I try to load the blog, I must add a trailing slash at the end of the URL to make it work. 但是,当我尝试加载博客时,必须在URL的末尾添加斜杠以使其正常工作。

http://www.example.com/blog >> 403 Forbidden error http://www.example.com/blog >> 403禁止的错误

http://www.example.com/blog/ >> Loads correctly http://www.example.com/blog/ >>正确加载

I tried to add trailing slash after URLs using htaccess, But I dont know is this the best solution or not? 我尝试使用htaccess在网址后添加尾部斜杠,但我不知道这是否是最佳解决方案? Could anyone suggest a way to ignore blog folder to be effected by beautiful url? 谁能建议一种忽略博客文件夹的方法,以使美丽的url生效?

解决这个问题的简单方法是创建一个名为blog.php的文件,该文件仅重定向到example.com/blog/。

RewriteCond %{REQUEST_URI} !^/blog

above the RewriteRules. 高于RewriteRules。

Not tested it, I think it should work 没有测试过,我认为应该可以

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

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