change root folder, redirect subdomain to subfolder, and ...

Discuss practical ways rearrange URLs using mod_rewrite.

change root folder, redirect subdomain to subfolder, and ...

Postby nbv4 » Sat Aug 16, 2008 2:32 pm

I want to achieve three things in my .htaccess file:

1. Change any URL from the old domain to the new domain. That includes "forums.old.com/blah" to "forums.new.blah"

2. When the URL has no subdomain, it changes the "root" to a subdirectory. That means just plain ol' "new.com" or "www.new.com" points to "new.com/something". But if there is a subdomain, then...

3. Subdomains point to subfolders. "blah.new.com" points to "new.com/blah" But I want the subdomain to still be shown in the address bar.

The problem is getting these three rules to work together. I'dd admit, I don't know jack shit about mod_rewrite, my only hope is to just find snipplets online and copy/paste them. Here is what I have already:

Code: Select all
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php

Options +FollowSymLinks
RewriteEngine on

##### domain change

RewriteCond %{HTTP_HOST} ^(.*)dol-domain.net [NC]
RewriteRule ^(.*)$ http://new-domain.in/$1 [R=301,L]

##### change to the /logbook folder

RewriteRule ^$ /logbook/ [QSA,L]

RewriteCond %{HTTP_HOST} ^(www.)?new-domain\.in$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^logbook(/.*)?$ /logbook%{REQUEST_URI} [QSA,L]


######subdomain to subfolder

# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^(www\.)?new-domain\.in$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.new-domain\.in$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?new-domain\.in$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.new-domain\.in$ [NC]
RewriteRule ^(.*)$ /%2/$1 [QSA,L]

Each chunk of code works perfectly together, but when you have all three of them in there, they conflict with each other and end up not working. If you wane to see how this is working, follow the following links:

http://old-domain.net
http://new-domain.in


http://old-domain.net/logbook
http://new-domain.in/logbook

http://forums.old-domain.net
http://forums.new-domain.in

http://old-domain.net/forums
http://new-domain.in/forums
Last edited by nbv4 on Tue Feb 09, 2010 2:26 pm, edited 1 time in total.
nbv4
 
Posts: 2
Joined: Sat Aug 16, 2008 4:24 am

Postby richardk » Sat Aug 16, 2008 3:30 pm

Try
Code: Select all
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php

Options +FollowSymLinks

RewriteEngine On

##### Domain change
RewriteCond %{HTTP_HOST} ^(.+\.)?onlinelogbook\.net$ [NC]
RewriteRule ^(.*)$ http://%1flightlogg.in/$1 [R=301,L]

##### Change to the /logbook folder
# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} ^(www\.)?flightlogg\.in$ [NC]
RewriteCond %{DOCUMENT_ROOT}/logbook%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www.)?flightlogg\.in$ [NC]
RewriteRule !^logbook(/.*)?$ /logbook%{REQUEST_URI} [QSA,L]


##### Subdomain to subfolder
# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^(www\.)?flightlogg\.in$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.flightlogg\.in$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?flightlogg\.in$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.flightlogg\.in$ [NC]
RewriteRule ^(.*)$ /%2/$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nbv4 » Sat Aug 16, 2008 3:39 pm

awesome that works, you are awesome
nbv4
 
Posts: 2
Joined: Sat Aug 16, 2008 4:24 am

How to use those code

Postby truespace4u » Fri Sep 25, 2009 8:30 pm

Hi,

I have the same problem but I don't know how to use that code or where to use that code.

I am new to this.

Please tell me how to use the code you provided and where to use it.
truespace4u
 
Posts: 4
Joined: Fri Sep 25, 2009 8:16 pm

Postby richardk » Sat Sep 26, 2009 1:33 pm

You would put it in a .htaccess file in your document root (ie. example.com/.htaccess). You also need to change things such as the domain name (flightlogg\.in) and sub directories (logbook).

Are you sure that is exactly what you want?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby truespace4u » Sat Sep 26, 2009 10:59 pm

Hi Richard Thanks for the replay.

Actually my problem is I have hosted a website in www.one.com.

With this provider If I create a directory in root, that can be accessed as subdomain

ex: If I create a folder sudomain1 then I can access it as

www.mydomain.com/sudomain1
or

http://subdomain1.mydomain.com

Now I have written the A record (*.mydomain.com & mydomain.com ) to one of my Static IP where I am using Apache web server.

Now I need rite .htaccess codes so that If I create a folder subdomain1 in httpdocs folder then I should be able to access it like
http://subdomain1.mydomain.com

Please assume my website name as mydomain.com
If it is necessary to write the A record for www.mydomain.com to my static IP, I am ready to do that.

Please help me out
truespace4u
 
Posts: 4
Joined: Fri Sep 25, 2009 8:16 pm

Postby richardk » Sun Sep 27, 2009 10:27 am

Please assume my website name as mydomain.com

example.com

What happens if you go to sub1.example.com at the moment?

Try the following in .htaccess file in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^.*$ /%2/$0 [QSA,L]

(FAQ: Virtual sub domains.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

I am getting internal server error 500

Postby truespace4u » Mon Sep 28, 2009 10:48 pm

Dear Friend,
I copied your codes to .htaccess in root directory and I changed the example.com to my exact domain.... But I am receiving this error.


500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@flamboyanz.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.0.63 (Win32) Server at example.com Port 80



and in error.log I found this...



[Tue Sep 29 12:06:05 2009] [alert] [client 219.64.153.123] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Tue Sep 29 12:11:45 2009] [alert] [client 219.64.153.123] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Tue Sep 29 12:14:11 2009] [alert] [client 219.64.153.123] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Tue Sep 29 12:14:12 2009] [alert] [client 219.64.153.123] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Tue Sep 29 12:14:14 2009] [alert] [client 219.64.153.123] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
[Tue Sep 29 12:15:31 2009] [alert] [client 121.241.48.204] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration, referer: http://images.google.com/imgres?imgurl= ... kAWUmum1BQ
[Tue Sep 29 12:15:47 2009] [alert] [client 192.168.0.14] D:/fwb/.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
truespace4u
 
Posts: 4
Joined: Fri Sep 25, 2009 8:16 pm

Problem solved, Thanks for the help

Postby truespace4u » Tue Sep 29, 2009 4:17 am

Dear Friend,

The Code worked, Awesome... Thank you so much...

I just commented the mode rewrite line...

and your code worked !!!


Take care
truespace4u
 
Posts: 4
Joined: Fri Sep 25, 2009 8:16 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 22 guests

cron