【问题标题】:Need to write multiple htaccess rule with multiple parameters需要编写具有多个参数的多个 htaccess 规则
【发布时间】:2015-04-30 05:17:02
【问题描述】:

我已经在 htaccess 中编写了一些将 .php 扩展名转换为 HTML 的规则,它们工作正常,但现在我需要为具有相同参数的不同页面编写规则,但如果我为不同页面设置相同的规则,它就不起作用它在同一页面上重定向。

下面是我的htaccess文件代码

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/

# add everything for webservice/...
RewriteRule ^webservice(/.*)?$ - [L,NC]

#redirect localhost/apnaujjain/blog.php?page_id=1&post_id=1&action=blog to localhost/apnaujjain/1/1/blog.html

#RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
#RewriteRule . %1/%2/%3.html? [R=301,L]
#RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ blog.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]

#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html

RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]

#redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to localhost/apnaujjain/1/contacts.html

RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

#redirect localhost/apnaujjain/contacts.php to localhost/apnaujjain/contacts.html

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
#RewriteRule ^(admin|webservice)($|/) - [L]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]

我已经就每条规则的作用写了评论。现在目前所有规则都在工作,但是当我添加其他规则时它停止工作,所以我只是评论了该规则。

page.php 的规则有效,但是当我将页面更改为 blog.php 时,某些规则有效,但带有多个参数且带有 blog.php 的某些规则无效。

非常感谢任何帮助

【问题讨论】:

  • 这实际上是不可能的,因为 blog.phppage.php 的漂亮 URI 方案现在很相似。
  • 所以我们不能强制 url 重定向到我们想要的位置,或者让它像 page.php 一样静态,所以它重定向到 page.php 和 blog.php 到 blog.php 或者我可以改变blog.php 中的参数名称(page_id 到页面)所以它会起作用吗?
  • 您好 Anubhava,请帮助它需要工作的最后一件事,每个 url 现在都工作正常只需将localhost/apnaujjain/blog_detail.php?post_id=1&action=blog 更改为localhost/apnaujjain/1/blog,请帮助其紧急我无法解决它,请尽快回复....谢谢

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

您可以使用此规则将localhost/apnaujjain/1/blog 路由到http://localhost/apnaujjain/blog_detail.php?post_id=1&action=blog

RewriteCond %{THE_REQUEST} /blog_detail\.php\?post_id(\d+)&action=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2.html? [R=302,L,NE]

RewriteRule ^(\d+)/(blog)\.html$ blog_detail.php?post_id=$1&action=$2 [L,QSA,NC]

【讨论】:

  • 抱歉,这条规则不起作用,还有一件事如果想重定向到localhost/apnaujjain/1/blog.html,那该怎么做呢?
  • 我删除了所有其他规则,然后尝试您建议的规则,但仍然无法正常工作,实际上没有任何重定向只是显示 blog_detail 页面,我认为重定向现在不起作用我不知道为什么。跨度>
  • 好吧抱歉误会了,当我输入localhost/apnaujjain/blog_detail.php?post_id=1&action=blog时我需要,然后它应该重定向到localhost/apnaujjain/1/blog.html
  • 你好 Anubhava,我又遇到了关于 codeigniter 中 htaccess 的问题。请查看这个问题 stackoverflow.com/questions/31043274,我相信你可以帮助我
  • 您好 Anubhava,很抱歉打扰您,但我再次卡在 .htaccess 文件中。请调查这个问题。它真的很紧急。请在有时间的时候看看这个问题。提前致谢。网址:stackoverflow.com/questions/31913023/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 2021-09-17
  • 2016-07-07
  • 1970-01-01
  • 2013-12-26
相关资源
最近更新 更多