wordpress - 使用 .mod_rewrite 将特定参数重定向到子目录
问题描述
我最近在我现有的 http://localhost/mysite PHP 网站上的子文件夹(博客)中添加了 WordPress,我已经为 &_GETS 请求重写了 URL,当我输入 http://localhost/mysite/blog/ 时,WordPress 网站工作正常但是当我输入 http://localhost/mysite/blog 它仍然有效,但 URI 更改为 http://localhost/mysite/blog/?id=blog 。任何帮助将不胜感激。
简而言之
我想要 http://localhost/mysite/blog
使用 .htaccess更改为 http://localhost/mysite/blog/
但它会自动更改为 http://localhost/mysite/blog/?id=blog
.htaccess
RewriteEngine on
##Rewrite for downloading page##
RewriteRule ^download/([0-9]+) /mysite/download.php?file=$1 [NC]
RewriteRule ^downloadpdf/([0-9]+) downloadpdf.php?file=$1 [NC]
#For sitemap.xml
RewriteRule ^sitemap.xml sitemap.php [NC]
#This section is for forms url rewriting for course
RewriteRule ^([^/.]+)$ index.php/?id=$1 [NC]
RewriteRule ^([^/.]+)/([^/.]+)$ index.php/?id=$1&course=$2 [NC]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)$ index.php/?id=$1&course=$2&stream=$3 [NC]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)$ index.php/?id=$1&course=$2&stream=$3&subject=$4 [NC]
#Custom pages pretty URL's
RewriteRule ^about-us about-us.php [NC]
RewriteCond %{QUERY_STRING} ^id=read$ [NC]
RewriteRule ^([^/.]+)$ read/index.php [NC,L,R=301]
ErrorDocument 404 /mysite/errors/not-found.php
解决方案
根据您显示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。
RewriteEngine ON
##Rewrite for downloading page##
RewriteRule ^download/([0-9]+)/?$ /mysite/download.php?file=$1 [NC,L]
RewriteRule ^downloadpdf/([0-9]+)/?$ downloadpdf.php?file=$1 [NC,L]
#For sitemap.xml
RewriteRule ^sitemap\.xml sitemap.php [NC,L]
#This section is for forms url rewriting for course
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/?$ index.php/?id=$1 [NC,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php/?id=$1&course=$2 [NC,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ index.php/?id=$1&course=$2&stream=$3 [NC,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ index.php/?id=$1&course=$2&stream=$3&subject=$4 [NC,L]
#Custom pages pretty URL's
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^about-us about-us.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=read$ [NC]
RewriteRule ^([^/.]+)$ read/index.php [NC,L,R=301]
ErrorDocument 404 /mysite/errors/not-found.php
OP 尝试的代码中的修复:
- 没有
L
用于规则的标志,因此即使匹配条件进行了重写,它们也会继续前进,这就是我认为在您的 url 的情况下发生的情况。 - 没有添加条件,所以当没有更多条件时,它会捕获任何不是理想情况的 uris,所以我给它添加了条件。
- 正则表达式的左侧部分也缺少尾随(可选)斜杠,我现在已经添加了它们。
- 点也没有转义,所以在规则中也转义了点。