dtpwra8456 2017-04-18 09:52
浏览 35
已采纳

在htaccess中重写规则将查询字符串转换为斜线php

OK I've tried multiple solutions but still can't achieve it. so what I need to do is

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

to

mydomain.com/search/product/london

Basically I'm using rewrite rules to hide .php extension by following this code

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

So now, search.php converts into search and when user searches. it redirects to trailing slash instead of query string

Here what I've used so far

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]

AND

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]

But both of them doesn't work. I don't have so much knowledge about so please help me out.

  • 写回答

3条回答 默认 最新

  • dongqieli4164 2017-04-18 16:12
    关注

    Have your site root .htaccess as this:

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效