【问题标题】:Rewrite Rule in htaccess convert query string into slashes phphtaccess中的重写规则将查询字符串转换为斜杠php
【发布时间】:2017-04-18 09:52:23
【问题描述】:

好的,我尝试了多种解决方案,但仍然无法实现。所以我需要做的是

mydomain.com/search?q=product&l=london

mydomain.com/search/product/london

基本上我正在使用重写规则通过遵循此代码来隐藏 .php 扩展名

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^([^\.]+)$ $1.php [NC,L]

所以现在,search.php 转换为搜索和用户搜索时。它重定向到斜杠而不是查询字符串

这是我目前使用过的

RewriteCond %{THE_REQUEST} /search/?q=([^&]+)&l=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]

#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d

 #skip the rule if the request is for file
 RewriteCond %{REQUEST_FILENAME} !-f
 #rewrite any other request  to "/packagemenu.php?"
 RewriteRule ^([^/]+)/([^/]+)/?$ /search?q=$1&l=$2 [NC,L]

RewriteBase /search/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+) [NC]
RewriteRule ^ search/%1/%2/? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal forward from pretty URL to actual one
RewriteRule ^search/([^/]+)/?$ search?q=$1 [NC,L,QSA]

RewriteRule ^search/([^/]+)/([^/]+)/?$ search?q=$1&l=$2 [NC,L,QSA]

但它们都不起作用。我对这方面了解不多,所以请帮帮我。

【问题讨论】:

  • 我认为这是一个愚蠢的错误,因为您有 GET 变量 location 但在正则表达式中搜索 l
  • 它是 l。对不起,我需要编辑获取变量是 q 和 l
  • search.php 是否与 .htaccess 处于同一级别并且都在您的网站根目录中?
  • 是的@anubhava 这就是为什么 search.php 成为唯一的搜索

标签: php apache .htaccess mod-rewrite


【解决方案1】:

让您的站点根目录 .htaccess 如下所示:

Options -MultiViews
RewriteEngine On
RewriteBase /jobportal/

# To externally redirect /search.php?q=product&l=london to /search/query/london/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/%2/? [R=301,L,NE]

# To externally redirect /search.php?q=product to /search/query/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=301,L,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ %1 [R=301,NE,L]

# ignore all rules below for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal forward from /search/product/ to search.php?q=product
RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA]

# internal forward from /search/product/london/ to search.php?q=product&l=london
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA]

# internal forward from /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

【讨论】:

  • 不,不是那样的。它不起作用我的 .htaccess 有第一个 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]
  • 那么没有规则
  • 你离布鲁赫这么近。只是它重定向到localhost/search/hello/adjk 而不是localhost/jobportal/search/hello/adjk 你能看一下吗
  • 第二个问题是当我点击搜索按钮时,它仍然会生成这样的 url mydomain.com/search?q=product&l=london 而不是 mydomain.com/search/product/london 请帮帮我我对 htaccess 的信息非常有限
  • 它正在重定向,但它说找不到对象看看图片imgur.com/G5G8Mv8
【解决方案2】:

您可以通过在.htaccess 中使用以下规则来实现 URL mydomain.com/search/product/london

RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)$ /search?q=$1&l=$2 [L]

请确保在测试之前清除缓存。

【讨论】:

  • 不行,它仍然重定向到搜索?q=product&l=london。我也清除了缓存并以隐身模式运行它,但不起作用
  • 很奇怪@SaadSuri ... 以上是您的整个.htaccess 内容吗?听起来您的其他规则之一正在导致您的重写中断。
  • 不,它低于我隐藏 .php 扩展名的一个重写规则,并且上面提到了该规则
  • 并且您尝试将规则置于删除 .php 的规则之上?
  • @thickguru 我认为他们正在寻找相反的东西....../search?q=product&l=london 是最初请求的 URL,大概是通过提交搜索表单来调用的。他们想将其重写为浏览器地址栏中显示的“漂亮”URL /search/product/london
【解决方案3】:

尝试如下,

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/search$
RewriteCond %{QUERY_STRING} ^q=([^\/]+)&l=([^\/]+)$
RewriteRule ^ search/%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^search/([^\/]+)/([^\/]+)$ search.php?q=$1&l=$2 [QSA,L]

【讨论】:

  • 什么是错误我没有看到你的完整 .htaccess 确保没有其他规则冲突。
  • 它现在可以工作了,但是当我输入 localhost/jobportal/search 时我怎么能重定向?q=hello&l=london。它会自动重定向到localhost/jobportal/search/hello/london
  • 或者当我点击搜索时它会将我重定向到 localhost/jobportal/search/hello/london
  • 可悲的是,它也不起作用。它现在也不适用于 localhost/jobportal/search/hello/london 给出错误对象未找到
  • 现在试试我自己测试过!
猜你喜欢
  • 1970-01-01
  • 2021-08-24
  • 2020-09-08
  • 1970-01-01
  • 2015-01-08
  • 2010-10-27
  • 2018-10-06
  • 2011-04-18
  • 1970-01-01
相关资源
最近更新 更多