Forum Moderators: phranque

Message Too Old, No Replies

,htaccess removing subfolder from url.

No idea how to do that.

         

darkalfx

12:36 am on Mar 6, 2011 (gmt 0)

10+ Year Member



My website is actually hosted in the subfolder /darkalfx

so when people go to my website I want to redirect them there, however I dont want to show /darkalfx/ after my website name on every page so I wish to remove it entirely from the URL.

This is my current .htaccess:

rewriteengine on
rewritecond %{HTTP_HOST} ^www.darkalfx.com$ [OR]
rewritecond %{HTTP_HOST} ^darkalfx.com$
rewriterule ^(.*)$ "http\:\/\/www\.darkalfx\.com\/darkalfx$1" [R=301,L]

How would I proceed to remove the subfolder from the URL?

BreathinBuddah

6:00 am on Mar 6, 2011 (gmt 0)

10+ Year Member



RewriteRule ^/?(.+)$ /darkalfx/$1 


I think. Untested, but that should basically move the entire url after .com/ to .com/darkalfx/

"there might be a slash at the start if it is there, remove it(lazy), capture everything after it" and then "add darkalfx before captured url"

Correct me if I'm wrong but that feels right. Also I think the R modifier makes it redirect the user to that url, and it sounds like you don't want that.

g1smd

9:19 am on Mar 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My website is actually hosted in the subfolder /darkalfx so when people go to my website I want to redirect them there, however I don't want to show /darkalfx/ after my website name on every page so I wish to remove it entirely from the URL.

A redirect tells the browser to request a new URL. The new URL will be shown in the browser URL bar.

What you need is a rewrite. A rewrite gets content from a different location inside the server, without revealing that location.

However, the first thing you must do is to link to the correct URLs that you want users to "see" and "use". It is links that define URLs.


RewriteRule ^/?(.+)$ /darkalfx/$1

This code accepts a URL request and silently fetches content from the /darkalfx/ folder. There is one problem. The new pointer matches the pattern again and will cause in infinite internal rewrite loop. You need to add a preceding RewriteCond to test that the request hasn't already been internally rewritten.

You need the leading slash when the code is in
httpd.conf
. In
.htaccess
the leading slash is not present. Requested paths and files are localised "per-directory" in .htaccess.

You must also add the [L] flag to every rule.


You also need additional rules so that if a user requests
(www.)example.com/darkalfx/<something>
they are redirected to
www.example.com/<something>
and if they request
example.com/<something>
they are redirected to
www.example.com/<something>
.

It is also a good idea to redirect calls for named index files to the correct canonical URL ending with a slash.

The rule order is very important. No request should ever see a double redirect.

For direct client external requests containing
/darkalfx
, index files are dealt with first, then everything else.

After that, other URLs are dealt with. Again, index files first, then everything else.

The first four rules ensure the user is requesting the correct URL. The final rule is the internal rewrite to fetch the content.

DirectoryIndex index.php index.html

# Externally redirect only direct client requests for /index.php and
# /index.html and /index.htm with /darkalfx to root URL ending with slash.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /darkalfx/([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^darkalfx/(([^/]+/)*)index.(html?|php)$ http://www.example.com/$1 [R=301,L]

# Externally redirect only direct client requests for /darkalfx<something>
# to www.example.com/<something>.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /darkalfx([^\ ]+)\ HTTP/
RewriteRule ^darkalfx(.*) http://www.example.com/$1 [R=301,L]

# Externally redirect only direct client requests for /index.php
# and /index.html and /index.htm to URL ending with slash.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index.(html?|php)$ http://www.example.com/$1 [R=301,L]

# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Internally rewrite requests for www.example.com/<anything>
# to internal server path at /darkalfx/<anything>
RewriteCond %{REQUEST_URI} !^/darkalfx
RewriteRule (.*) /darkalfx/$1 [L]


A redirect is a URL to URL translation. The user sees the change.

A rewrite is a URL to filepath translation. The user does not see the change.

Links to images, stylesheets and scripts should begin with a leading slash and include the full path from the root, e.g.
href="/images/image.png"
. Think of their location in terms of URLs.