方案
我正在审核已迁移到基于Wordpress的设置的网站,但无法访问Wordpress中的所有管理工具(它是外部托管/托管的),但我确实可以访问htaccess文件。现在,我需要做两件事:
挑战
但我想通过以下方式实现这一目标:
让我困难的是,当它既是旧网址又以www为前缀时,将重定向保持在最低限度。
我的下一个最大的问题是,我想使用重写映射,但似乎你必须在外部加载一些东西,而不是在htaccess文件中定义一个我更喜欢的字典。
更多详情
/index.php?page=<pageName>
。/a-new-url-example/
。mydomain.com/index.php?page=boots
到mydomain.com/boots/
www.mydomain.com/index.php?page=shoes
到mydomain.com/shiny-shoes/
(需要某种地图来处理shoes
- &gt; shiny-shoes
) 答案 0 :(得分:2)
我希望这不会与你在htaccess中的现有规则发生冲突,正如你提供的那样。
#exceptions first
RewriteCond %{QUERY_STRING} ^page=shoes3$ [NC]
RewriteRule ^index\.php$ /shiny-shoes/? [R=302]
#urls that directly map to the new url scheme
RewriteCond %{QUERY_STRING} ^page=(.*)$ [NC]
RewriteRule ^index\.php$ /%1/? [R=302]
#note the absence of the L flag in the above rules.
# from apache docs: the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset
# no-www (make sure this is the last rule)
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^http://[^/]+/(.*)$ http://%1/$1 [R=302,L]
答案 1 :(得分:0)
以下是一个例子:
<强>条件:强>
带有这些规则的.1 .htaccess文件位于根目录。
.2未在请求的URL中修改GET值(以下示例中的“值”)。换句话说,“鞋子”在请求的URL中是相同的,而不是问题中的“闪亮鞋子”。它是可行的,但需要在模式中包含别名列表,或者每个项目都有不同的规则。
.3根目录中的index.php脚本必须能够处理所有可能的值,并在未找到匹配项时加载正常页面(如果有)。
RewriteEngine on
Options +FollowSymLinks
#Redirect from http://www.mydomain.com to http://mydomain.com
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
#mydomain.com/value/ to mydomain.com/index.php?page=value
RewriteRule ^(.*)?/$ index.php?page=$1 [L]
要测试此示例,请在根目录中的index.php中包含以下唯一代码:
<?php
if ( $_GET[ 'page' ] == 'value' ) { // Change "value" accordingly
echo "Processing item <br /><br />";
}
else {
echo "Loading normal page<br /><br />";
}
?>