简体   繁体   中英

Hide a folder from the URI with RewriteRule in a .htaccess file

I would really appreciate if you could help me building my .htaccess file.
What I want to achieve here is moving all of my domain's files and folders to a root subfolder ("mydomain") and make sure that the URL visible to my visitors still looks like

domain.com/file.extention  

instead of

domain.com/mydomain/file.extention 

or

domain.com/subfolder/file.extention

instead of

domain.com/mydomain/subfolder/file.extention

This is my folders tree:

/
  /mydomain
    /subfolder
    index.php
    test.php

and here is my current .htaccess file:

<IfModule mod_rewrite.c>

# System symbolic links are allowed.
Options +FollowSymlinks

# Runtime rewriting engine enabled.
RewriteEngine On

#
# BEGIN DOMAIN
#

# Make 'mydomain' subfolder the root folder for the domain.
RewriteCond %{HTTP_HOST} ^((www\.)?domain\.com) [NC]
RewriteRule ^\/?$ mydomain/ [NC,L,S=1]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?mydomain/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ mydomain/$1 [NC,L]

#
# END DOMAIN
#

</IfModule>

The problem here is that it works when I browse for

domain.com/subfolder/

but not with

domain.com/subfolder

and with

domain.com/index.php

but not with

domain.com/test.php

Thank you all,
Matteo

Changing domain.com/mydomain/file.extention to domain.com/file.extention

use this for example

RewriteRule ^mydomain/(.+)$ http://www.domain.com/ [R=301,L]

But if you redirects working just for some pages check that you clear browser cache or use another one instead

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