使用 .mod_rewrite 将特定参数重定向到子目录

时间:2021-02-14 08:55:07

标签: wordpress apache .htaccess

我最近在我现有的 http://localhost/mysite PHP 网站的子文件夹(博客)中添加了 WordPress,我已经在其中重写了 &_GETS 请求的 URL,当我输入 http://localhost/mysite/ 时,WordPress 网站工作正常blog/ 但当我输入 http://localhost/mysite/blog 时它仍然有效,但 URI 更改为 http://localhost/mysite/blog/?id=blog 。任何帮助将不胜感激。

简而言之

我想要 http://localhost/mysite/blog

变成 http://localhost/mysite/blog/ 使用 .htaccess

但它会自动更改为 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

1 个答案:

答案 0 :(得分:1)

根据您显示的示例,您可以尝试以下操作吗?请确保在测试您的网址之前清除浏览器缓存。

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 情况下正在发生的情况。
  • 没有添加任何条件,因此当没有更多条件时,它将捕获任何类型的 uri,这不是理想情况,因此我向其添加了条件。
  • 我现在添加的正则表达式的左侧部分也缺少尾随(可选)斜杠。
  • 点也没有转义,所以在规则中也转义了点。
相关问题