.htaccess mod_rewrite: rewriting a request to a path

How can I use rewriting to change:

/? tag = foo

To:

/ tag / foo

I tried:

RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]

But that did not work.

+3
source share
3 answers

To avoid recursion, you should check the query string , since the query string in %{QUERY_STRING}can already be changed by another rule:

RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]

Then you can rewrite the queries back inside without conflict:

RewriteRule ^tag/(.*) index.php?tag=$1 [L]
+5
source

Try the following:

RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]

Typically, overwrites are used to achieve the opposite effect. You really don't want to do the following?

RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
+6
source

URL-, :

http://java.scandilabs.com/faq?key=Contents_of__gitigno

URL-, :

http://scandilabs.com/technology/knowledge/Contents_of__gitigno

Andrew , :

RewriteCond %{HTTP_HOST} ^java
RewriteCond %{QUERY_STRING} ^key=(.+)$    
RewriteRule ^(.*)$ http://scandilabs.com/technology/knowledge/%1? [R=301,L]

Please note that if you are using apache 2.4 or higher, you can use the QSD flag instead of a question mark.

0
source

Source: https://habr.com/ru/post/1714146/


All Articles