【问题标题】:Redirecting specific parameter to a subdirectory using .mod_rewrite使用 .mod_rewrite 将特定参数重定向到子目录
【发布时间】:2021-02-14 08:55:07
【问题描述】:

我最近在我现有的 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

【问题讨论】:

  • 你需要设置永久链接
  • 欢迎来到 SO,请在您的问题中分享您尝试过的 htaccess 文件,谢谢。

标签: wordpress apache .htaccess


【解决方案1】:

根据您显示的示例,您能否尝试以下操作。请确保在测试 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 中发生的情况。
  • 没有添加条件,所以当没有更多条件时,它会捕获任何类型的不理想情况的 uri,因此我为其添加了条件。
  • 我现在添加的正则表达式左侧也缺少尾随(可选)斜杠。
  • 点也没有转义,所以在规则中也转义了点。

【讨论】:

  • 非常感谢它的工作,有没有什么好的资源来学习正则表达式/htaccess,我想我不太了解这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2019-12-23
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多