Htaccess Silent Redirect to Subdirectory: Subdirectory showing when the trailing '/'

I dug high and low around Google and StackOverflow to try to figure out my problem by trying countless solutions, but nothing worked.

I am looking to move the network root of the primary domain on my server to a subdirectory. What I currently have for the server path to my web root:

/home/user/public_html/MyWebFilesHere

What I want to have:

/home/user/public_html/subdir/MyWebfilesHere

When I browse mydomain.com, there should be no apparent difference (ie "subdir" does not appear after the redirect).

Unfortunately, I will confine myself to this exclusively with the .htaccess file, since I am on a shared hosting and do not have access to the Apache configuration files and the like. :(

In my .htaccess in public_html, I have:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1 [L]

, .

mydomain.com/Contact/

, /subdir/Contact/, .

mydomain.com/Contact

( '/'), , ,

mydomain.com/subdir/Contact/

, , "subdir".

colincwilliams.com/Contact/

colincwilliams.com/Contact

- , , ?

+5
2

, - , mod_dir (, , . . DirectorySlash mod_dir

:

  • : mydomain.com/Contact
  • mod_dir , /Contact
  • /Contact /subdir/Contact
  • mod_dir , /subdir/Contact - , mydomain.com/subdir/Contact/
  • , /subdir/in.

DirectorySlash off .htaccess, mod_dir . , , . , , :

RewriteEngine on

# Has a trailing slash, don't append one when rewriting
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{THE_REQUEST} ./\ HTTP/1\.[01]$ [OR]
# OR if it a file that ends with one of these extensions
RewriteCond %{REQUEST_URI} \.(php|html?|jpg|gif|css)$
RewriteRule ^(.*)$ /subdir/$1 [L]

# Missing trailing slash, append one
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{THE_REQUEST} [^/]\ HTTP/1\.[01]$
# But only if it not a file that ends with one of these extensions
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif|css)$
RewriteRule ^(.*)$ /subdir/$1/ [L]

: !^/mydomain/ !^/subdir/, , , mod_rewrite (foo → /subdir/foo → /subdir/subdir/foo → /subdir/subdir/subdir/foo ..). , .

: . RewriteCond \.(php|html?|jpg|gif|css). , . / .

+7

Jon Lin , . :

, - , mod_dir (, , . . DirectorySlash mod_dir

:

  • : mydomain.com/Contact
  • mod_dir , /Contact
  • /Contact /subdir/Contact
  • mod_dir , /subdir/Contact - , mydomain.com/subdir/Contact/
  • , /subdir/in.

, /subdir DirectorySlash .

RewriteEngine /subdir, , mod_dir, /subdir, mod_dir .

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/exception1|exception2|...
RewriteRule ^(.*)$ /subdir/$1

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^subdir/(.*) $1/ [R,L]

. , , RewriteEngine , . , .

, , RewriteEngine, /subdir:

RewriteCond %{REQUEST_URI} ^subdir
RewriteRule .* - [L]
0

All Articles