简体   繁体   中英

Remove folder name from URL

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

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.

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM