3

次のようなURLがあるとします。

http://domain.com/?theme_of_site=parameter_value
http://domain.com/?type_of_site=parameter_value
  • domain.com-安定/静的、常に同じ
  • theme_of_sitetype_of_site-安定/静的、選択した場合と同じ
  • parameter_value-動的

これは、常に次のようなURLを持つようなものです。

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

どうすればこれを.htaccessで書くことができますか?


以下の議論のために:

# 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
4

1 に答える 1

2

クエリ文字列パラメータはでキャプチャする必要がありますRewriteCond。値を一致(theme|type)させてキャプチャしてから、inを使用して内部的に書き換えること(theme|type)_of_siteができ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]

上記の簡単な例は、いずれかのパラメーターが指定されている場合にのみ機能します。example.com/?type=abc&theme=123もう少し複雑になるように、両方を同時に処理できるようにする必要がある場合:

# 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]
于 2013-03-20T13:48:09.890 に答える