Remove folder name from URL

安稳与你 提交于 2021-02-11 18:02:17

问题


I have a domain www.domain.com redirected to / in my server.

Next I have index.php whith code:

header("Location: http://domain.com/v3/");

When I enter mydomain.com I have mydomain.com/v3/ in url.

How to remove v3 from Url


回答1:


Remove header line from your PHP code since it is doing a redirect and have this lookahead based rule in your root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+v3/([^?\s]*) [NC]
RewriteRule ^ /%1 [R=302,L,NE]

RewriteRule ^((?!v3/).*)$ /v3/$1 [L,NC]

Which basically means if request is not starting with /v3/ forward to /v3/ without changing the URL in browser.




回答2:


If you're redirecting from PHP, you can use parse_url to clean it up.

$url = "http://domain.com/v3/";

$host = parse_url( $url, PHP_URL_HOST); 
$scheme = parse_url( $url, PHP_URL_SCHEME); 

header( $scheme . '://' . $host );


来源:https://stackoverflow.com/questions/22443921/remove-folder-name-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!