如何在.htaccess中创建别名以更改本地URL


How to make an alias in .htaccess to change a local URL

我很确定这是重复的,但我不知道如何找到这个简单问题的答案。

所以我有一个这样的本地页面:

http://example.com/mypage.php

我想做的就是让任何试图访问http://example.com/subfolder/mypage.php的人在上面的位置实际加载页面。

我不想重定向。http://example.com/subfolder/mypage.php在我的服务器上并不作为实际路径存在,但我想通过.htaccess 中的规则使其可用

试试这个:

RewriteEngine on
RewriteRule ^subfolder/(.*) /$1

此规则将从/subfolder/开始的每个请求重写为/。或者,如果您只需要特定的URL路径:

RewriteEngine on
RewriteRule ^subfolder/mypage.php /mypage.php

您可以将其添加到.htaccess 中

RewriteEngine On
RewriteRule  ^([^/]+)/(.*)   /$2

或者像一样直接在httpd.conf中使用

<Directory /var/www/>
    Options Indexes FollowSymLinks ExecCGI
    AllowOverride None
    Order deny,allow
    Allow from all  
    RewriteEngine On
    RewriteRule  ^([^/]+)/(.*)   /$2    
</Directory>

使用$1获取子文件夹,使用$2获取mypage.php:)