.htaccess rule, rewrite key in url

坚强是说给别人听的谎言 提交于 2020-12-10 07:53:31

问题


Let's say we have an url like this:

http://domain.com/?theme_of_site=parameter_value
http://domain.com/?type_of_site=parameter_value
  • domain.com - stable/static, always the same
  • theme_of_site & type_of_site - stable/static, the same when choosen
  • parameter_value - dynamic

It's like to have always the url's like:

http://domain.com/?theme=parameter_value
http://domain.com/?type=parameter_value

How can I write this in .htaccess?


For the below discussion:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Start by redirecting the original links from type_of_site= to type=
# Only if the browser's original request contains the strings
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1&type=%2 [L,R=301]

RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1 [L,R=301]

RewriteCond %{THE_REQUEST} type_of_site=([^&]+)    
RewriteRule (.*) $1?type=%1 [L,R=301]

# Then, after performing the initial redirects to change the browser
# URL, rewrite the parameters internally

# Match both present:
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1&type_of_site=%2 [L]

# Then match each individually
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1 [L]

RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?type_of_site=%1 [L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

回答1:


Query string parameters need to be captured in RewriteCond. You can match (theme|type) and capture the value, then rewrite it internally using the (theme|type)_of_site in a RewriteRule.

RewriteEngine On
# Match the theme or type paramater in `%1` and capture its value in `%2`
RewriteCond %{QUERY_STRING} (theme|type)=([^&]+)
# Rewrite The parameter name to include _of_site using the values captured above
# for all URLs as captured in $1
RewriteRule (.*) $1?%1_of_site=%2 [L]

The simple example above works only if either of the parameters is specified. If you need to be able to handle both at once like example.com/?type=abc&theme=123 it gets a little more complex:

# Start by redirecting the original links from type_of_site= to type=
# Only if the browser's original request contains the strings
RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteCond %{THE_REQUEST} type_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1&type=%2 [L,R=301]

RewriteCond %{THE_REQUEST} theme_of_site=([^&]+)
RewriteRule (.*) $1?theme=%1 [L,R=301]

RewriteCond %{THE_REQUEST} type_of_site=([^&]+)    
RewriteRule (.*) $1?type=%1 [L,R=301]

# Then, after performing the initial redirects to change the browser
# URL, rewrite the parameters internally

# Match both present:
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1&type_of_site=%2 [L]

# Then match each individually
RewriteCond %{QUERY_STRING} theme=([^&]+)
RewriteRule (.*) $1?theme_of_site=%1 [L]

RewriteCond %{QUERY_STRING} type=([^&]+)
RewriteRule (.*) $1?type_of_site=%1 [L]


来源:https://stackoverflow.com/questions/15525227/htaccess-rule-rewrite-key-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!