Rewrite Rule in htaccess convert query string into slashes php

后端 未结 3 791
一整个雨季
一整个雨季 2020-12-20 05:54

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

t

相关标签:
3条回答
  • 2020-12-20 06:35

    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]
    
    0 讨论(0)
  • 2020-12-20 06:39

    Try as below,

    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]
    
    0 讨论(0)
  • 2020-12-20 06:51

    You can achieve the URL mydomain.com/search/product/london by using the following rule in your .htaccess.

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

    Just make sure you clear your cache before testing this.

    0 讨论(0)
提交回复
热议问题