Forum Moderators: phranque

Message Too Old, No Replies

any sub folder to specific location using htaccess

want to redirect any sub folder to a specific location with refference

         

silverfh

3:35 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



Hi Guys,

I have a simple condition to apply but its getting most complicated to me..

I want to redirect any user that comes to any sub folder of my site to a specific location with the name of that sub folder..
it works like that

RewriteRule ^([^/]+).com/([^/]+).php/website/$2.php?user=$1&file=$2[L,QSA]

the prob is to make above statement work I have to browse it like that
http://example.com/user.com/index.php

the thing is that I want to remove (.com) from there
it should be http://example.com/user/index.php

and i need to get values like this..
http://example.com/webite/index.php?user=user&file=index

I tried it this way.
RewriteRule ^([^/]+)/([^/]+).php/website/$2.php?user=$1&file=$2[L,QSA]

but this gives me a 500 error.

can anyone help please?

also what if I want to use user as a sub domain?
http://user.example.com/index.php
and this one should read as
http://example.com/webite/index.php?user=user&file=index

Please do help.. my whole project is depending on it..

Thanks in advance

[edited by: jdMorgan at 4:45 pm (utc) on Jan. 24, 2008]
[edit reason] example.com [/edit]

jdMorgan

5:17 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The domain name is not part of the local URL-path 'seen' by RewriteRule. You also have syntax errors and regular-expressions errors. A review of the documentation and the URL Rewriting Guide on the Apache site would have been worthwhile in this case -- It would probably have saved you a lot of time.

When you get a 500-Server Error, look at your server error log file. It will often tell you exactly what is wrong.

Your example code is inconsistent with your stated requirements, and the distinction between fixed values and variables is muddied by use of the same strings as both variables and query-string names, but here is a best-guess:

> http://example.com/<username>/<filename>.php --> /website/index.php?user=<username>&file=<filename>


RewriteCond $1 !^website/
RewriteRule ^([^/]+)/([^.]+)\.php$ /website/index.php?user=$1&file=$2 [QSA,L]

> also what if I want to use user as a sub domain?
> http://<username>.example.com/<filename>.php --> /website/index.php?user=<username>&file=<filename>


RewriteCond $1 !^website/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^a-z][a-z0-9\-]+[a-z0-9])\.example\.com
RewriteRule ^([^.]+)\.php$ /website/index.php?user=%1&file=$1 [QSA,L]

Note that in this example, the subdomain/username is checked and required to be valid according to the requirements of the HTTP protocol. In this implementation, it is also required to consist of at least three characters. A subdomain/username of "www" is not allowed.

In both examples, the first RewriteCond is used to prevent an "infinite" rewriting loop.

For non-php requests to the users' space, you may need another Rewrite to handle things like user images, CSS files, external JavaScripts, and robots.txt files. This depends on whether I understood your original intent.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com]. You may also find our site search to be very useful to find previous posts where your questions have already been answered.

Jim

[edited by: jdMorgan at 5:20 pm (utc) on Jan. 24, 2008]

silverfh

9:01 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



Thank You Jim..

for so much detailed answer.. really appriciated.

offcourse you saved lots of my time.. as you can see em not a pro in htaccess, I just know a few tricks and thats it..

What actually I am doing is that em creating user's on the fly.. through a master admin account, and assigning them different templates design according to what they select, from there own administration section.

so it will be either user1.site.com, user2.site.com
or site.com/user1, site.com/user2

I have been searching for valuabel link on htaccess that teaches one from the begning and in details, so that a non-pro like me can easily learn, but wasnt able to get proper results as I was looking.
I believe your post will solve much of my prob. and offcourse the helping links you sent will help me learning more about htaccess.

I'm playing a lil with what you just suggested, and surly wil post back here of whatever results i get..

may be this may help somsone else like me as well..

Thanks Again
Faheem

silverfh

2:42 pm on Jan 26, 2008 (gmt 0)

10+ Year Member



Sill cant make it work ..

Thats what I am doing

URL > http://example.com/user1/index.php

HTACCESS >

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
AddType application/x-httpd-php .html .htm

RewriteCond $1 !^website/
RewriteRule ^([[^/]]+)/([[^.]]+)\.php$ /website/index.php?user=$1&file=$2 [[QSA,L]]

ERROR LOG:

[error] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
[error] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

I have a folder name website on root with an index.php in it
it by simply adding any string to it.. like .com or something

RewriteCond $1 !^website/
RewriteRule ^([[^/]]+).com/([[^.]]+)\.php$ /website/index.php?user=$1&file=$2 [[QSA,L]]

and the access URL will be

http://example.com/user1.com/index.php

em doing some search on this form as well but if you can help me in the mean time please.. I really need to solve this today .. my 2 projects are pending just coz of that..

Thanks

[edited by: jdMorgan at 4:42 pm (utc) on Jan. 26, 2008]
[edit reason] example.com [/edit]

jdMorgan

4:40 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please review the code posted above -- Your regex patterns and flag syntax appear to differ critically from what was posted, and will cause errors.

With mod_rewrite, even a single incorrect character will cause problems, and some quite severe.

We may have a character-encoding problem here; Try selecting ISO-8859-1 character encoding in your browser if you suspect that this may be the case.

Jim

silverfh

7:27 pm on Jan 26, 2008 (gmt 0)

10+ Year Member



well the char code is fine.. its fine in my htaccess file.. dont knwo why its like that here.. may be when i was copying it or something..
anyway.. my prob is resolved.. I just updated your code a lil after reading some other posts and other haccess help.. and it worked perfact with no errors

RewriteCond %{REQUEST_FILENAME}!^(.*)\.php(.*)$
RewriteRule ^([^/]+)$/$1/index.php?user=$1&file=index[L,QSA]

RewriteCond %{REQUEST_URI}!^/website/¦/admin/¦/masterAdmin/(.*)$
RewriteRule ^([^/]+)/([^.]+)\.php$ /website/$2.php?user=$1&file=$2 [QSA,L]

what I think em doing here is :)
em checking if user is accessing a FOLDER not a file directly then take that folder as user name and send it to index file under that folder.. that will prevent me activating 2nd condition if the path is real like em using admin and masterAdmin folders for administration panels.

2nd em simply redirecting that to user site

Thanks again for your help Jim
I think i learned lil further about htaccess today.. its actually not that hard as it looks like :)