htaccess 重写当前正确的 URL 以显示为其他内容,如果输入了其他 URL,请重定向到此 URL



嘿,所以我正在尝试让我的htaccess文件做两件事:

1)如果用户输入http://dstealth.com/ihttp://dstealth.com/i/some/other/address我希望htaccess文件使用http://dstealth.com/z_index/http://dstealth.com/z_index/some/other/address

2)我还希望htaccess文件重定向用户,如果他们使用旧地址。例如,如果用户键入http://dstealth.com/z_indexhttp://dstealth.com/z_index/some/other/address我希望URL显示为http://dstealth.com/i/some/other/address

基本上在任何时候都将z_index替换为i,即使目录i不存在并且z_index仍然是正在使用的目录。

我已经尝试了很多东西,但它似乎不起作用,因为它不断将我重定向到我的 404 页面。

我的 .htaccess 文件:

ServerSignature Off 
ErrorDocument 403 /error_pages/403.php
ErrorDocument 404 /error_pages/404.php
ErrorDocument 500 /error_pages/500.php
IndexIgnore *
RewriteEngine On    # Turn on the rewriting engine - only needed once
Options -MultiViews
# Prevent directory listings
Options All -Indexes
# Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
# Rewrite www.dstealth.com as dstealth.com
RewriteCond %{HTTP_HOST} ^www.dstealth.com [NC]
RewriteRule ^(.*)$ http://dstealth.com/$1 [R=301,NC]
# Rewrite /z_index/ as /i/
RewriteRule ^z_index/([^/.]+)$ /i/$1 [NC]
RewriteRule ^i/([^/.]+)$ /z_index/$1 [NC,L]

结果:

  • http://dstealth.com/z_index/仍然看起来完全相同,不会更改为http://dstealth.com/i/

  • http://dstealth.com/i/把我带到我的404页:(

我相信我通过以下代码得到了我想要的。如果有任何错误或效率低下,请告诉我:

RewriteCond %{THE_REQUEST} s/z_index [NC]
RewriteRule ^z_index/([^.]+).php$ /i/$1.php [R=302,L]
RewriteCond %{THE_REQUEST} s/z_index [NC]
RewriteRule ^z_index/([^.]+)$ /i/$1 [R=302,L]
RewriteCond %{THE_REQUEST} s/z_index [NC]
RewriteRule ^z_index/?$ /i/ [R=302,L]
RewriteRule ^i/([^.]+).php$ /z_index/$1.php [NC,L]
RewriteRule ^i/$ /z_index/?&%{QUERY_STRING} [NC,L]

相关内容

最新更新