简体   繁体   中英

RewriteCond %{QUERY_STRING}

Here's what I'm trying to accomplish:

http://example.com/real-estate/?group=rentals needs to go to http://example.com/real-estate/rentals

Here's what I have in my .htaccess that isn't working:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/real-estate/ [NC]
RewriteCond %{QUERY_STRING} ^group=rentals
RewriteRule (.*) http://example.com/real-estate/rentals/? [NC,R=301,L]
</IfModule>

This is very unusual... Typically people want the redirection the other way around....

Your code looks nearly fine, only some minor corrections. But those might be what you are missing:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_URI} ^/real-estate/?$ [NC]
  RewriteCond %{QUERY_STRING} ^group=rentals$
  RewriteRule ^ http://example.com/real-estate/rentals [R=301,L]
</IfModule>

For this to work the rules have to either be placed directly inside the http hosts configuration, or inside a .htaccess style file in the hosts document root with enabled interpretation of such files.

And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Just use this in your .htaccess file:

RewriteEngine On
RewriteRule ^real-estate/([^/]*)$ /real-estate/?group=$1 [L]

It will leave you with the URL: http://example.com/real-estate/rentals . Make sure you clear your cache before you test this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM