正则表达式 – htaccess:将旧域和所有页面重定向到新域
我知道Stackoverflow上有很多例子,但我仍然想念一些东西.
我正在尝试使用以下规则将http://old.domain.com/fr/重定向到http://brand.new-domain.com/fr/,但这不起作用: # Enable Rewrite Engine RewriteEngine On RewriteBase / # Add a trailing slash to paths without an extension RewriteCond %{REQUEST_METHOD} !=POST RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$ RewriteRule ^(.*)$$1/ [L,R=301] # Redirect domain Options +FollowSymLinks RewriteCond %{HTTP_HOST} ^old.domain.com [OR] RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC] RewriteRule ^(.*)$http://brand.new-domain.com/$1 [r=301,L] # Remove index.php # Uses the "exclude method" # http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method # This method seems to work best for us,you might also use the include method. # http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method # Exclude root files RewriteCond $1 !^(index.php) [NC] # Exclude EE folders RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC] # Exclude user created folders RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC] # Exlude favico,robots,ipad icon RewriteCond $1 !^(favicon.ico|robots.txt|pple-touch-icon.png) [NC] # Remove index.php RewriteCond %{QUERY_STRING} !^(ACT=.*)$[NC] RewriteCond %{QUERY_STRING} !^(URL=.*)$[NC] RewriteRule ^(.*)$/index.php?/$1 [L] 它在我调用根URL时正确地重定向,但在我调用页面时却没有.我究竟做错了什么? 提前致谢! 光伏
在编写mod_rewrite规则时,规则将按照它们出现的顺序应用.
要将旧域重定向到新域,您需要将该规则放在.htaccess或httpd.conf文件中的第一个 – 所有其他规则应该出现在它之后. 如果您只想重定向某个目录,则以下规则将执行此操作,同时允许该站点的其余部分正常运行: <IfModule mod_rewrite.c> RewriteEngine On # Redirect Only Matching Directories RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$ RewriteRule ^(.*)$http://brand.new-domain.com/fr/$1 [R=301,L] </IfModule> 如果要重定向整个站点,以下规则将执行此操作: <IfModule mod_rewrite.c> RewriteEngine On # Redirect Entire Site to New Domain RewriteCond %{HTTP_HOST} ^old.domain.com$[OR] RewriteCond %{HTTP_HOST} ^other-old.domain.com$[NC] RewriteRule ^(.*)$http://brand.new-domain.com/$1 [R=301,L] </IfModule> 如果您关心让抓取工具知道您的内容已移动并希望尽可能无缝地进行转换,请确保将301 Redirect标记保留在RewriteRule中. 这将确保用户和搜索引擎定向到正确的页面. 虽然我们正在讨论这个问题,但作为EE 2.2版本的一部分,EllisLab现在“正式”为removing index.php from ExpressionEngine URLs提供有限的技术支持. 只需将您的代码添加或更新为以下内容,请务必考虑您已有的任何规则: <IfModule mod_rewrite.c> RewriteEngine On # Removes index.php RewriteCond $1 !.(gif|jpe?g|png)$[NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$/index.php/$1 [L] # If 404s,"No Input File" or every URL returns the same thing # make it /index.php?/$1 above (add the question mark) </IfModule> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |