.htaccess/mod_rewrite: West <--- Main Site ---> East - .htaccess

I would really appreciate anyone's help with a mod_rewrite question. I don't know regex and am not familiar with .htaccess directives, so I can't think of how to solve the problem I am having.
Below is a description of what I am trying to accomplish...
3 websites:
http://www.example.com/ or http://example.com/
http://east.example.com/
http://west.example.com/
Have pages on main site that need to be redirected to other sites:
http://www.example.com/location1
http://www.example.com/location2
http://www.example.com/location3
http://www.example.com/location4
http://www.example.com/location5
http://www.example.com/location6
Need to redirect some pages to one website based on location, e.g http://east.example.com/:
http://www.example.com/location1 redirect to http://east.example.com/location1
http://www.example.com/location2 redirect to http://east.example.com/location2
http://www.example.com/location3 redirect to http://east.example.com/location3
(also without www) e.g.
http://example.com/location1 redirect to http://east.example.com/location1
Need to redirect some pages to another website based on location, e.g http://west.example.com/:
http://www.example.com/location4 redirect to http://west.example.com/location4
http://www.example.com/location5 redirect to http://west.example.com/location5
http://www.example.com/location6 redirect to http://west.example.com/location6
(also without www) e.g.
http://example.com/location4 redirect to http://west.example.com/location4
Want a rewrite rule that works like:
If domain name is www.example.com or domain name is example.com
And domain name is not east.example.com
And domain name is not west.example.com
And "somelocation" (in the east) is in the URL
Then Rewrite(Redirect) URL to http://east.example.com/.../somelocation/.../somepage
NOTE: the three websites actually share the same codebase, so the URLs all point to the same location/.htaccess file. So, I tried using a basic redirect, and ended up with an error that said... can't open page because of too many redirects.
If anyone knows the answer and can help with this, I would really appreciate the help!
EDIT: Example of expected results
Original URL:
"http://www.example.com/locations/eastend-web-page"
Rewritten URL:
"http://east.example.com/locations/eastend-web-page"

Please try to fill a rewritemap file (see here) to make a correspondance with the URLs that are part of your "east" domain. You may declare it like this:
RewriteMap mapmaintoeast \
dbm:/web/htdocs/yoursite/rewriterules/mapmaintoeast.map
Regarding your sample, your map file may be filled with things like:
eastlocation1 mainlocation1
eastlocation2 mainlocation2
eastlocation3 mainlocation3
...
I've called them 'east' and 'main' to distinguish clearly. These examples are supposed to be URLs.
Do the same for west:
RewriteMap mapmaintowest \
dbm:/web/htdocs/yoursite/rewriterules/mapmaintowest.map
In that file:
westlocation1 mainlocation4
westlocation2 mainlocation5
westlocation3 mainlocation6
...
Then here you go for the hard part:
# This cond put any non-empty string into "%1" (= parenthesis + first cond):
RewriteCond %{QUERY_STRING} (.+)
# The following rule doesn't touch the URL, but
# will try to search into the map file and
# create fill an environment variable called MAINTOEAST with the
# string found and if not found, assign MAINTOEAST to "notfound"
RewriteRule . - [QSA,E=MAINTOEAST:${mapmaintoeast:%1|notfound}]
# if MAINTOEAST not empty and different from "notfound":
RewriteCond %{ENV:MAINTOEAST} !^$
RewriteCond %{ENV:MAINTOEAST} !(notfound)
# ok found => redirect to east:
RewriteRule (.*) http://east.example.com$1 [QSA,R=301,L]
# Now do the same with west (no comments needed (see previous)):
RewriteCond %{QUERY_STRING} (.+)
RewriteRule . - [QSA,E=MAINTOWEST:${mapmaintowest:%1|notfound}]
RewriteCond %{ENV:MAINTOWEST} !^$
RewriteCond %{ENV:MAINTOWEST} !(notfound)
RewriteRule (.*) http://west.example.com$1 [QSA,R=301,L]
This should work.
I hope I've given you enough clues to finish the job ;)
If you don't have enough clues...
Two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

RewriteEngine On
# If domain name is www.example.com or domain name is example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
# And "somelocation" (in the east) is in the URL,
# Then Rewrite(Redirect) URL to http://east.example.com/.../somelocation/.../somepage
RewriteRule ^(.*somelocation.*)$ http://east.example.com/$1 [R,L]
You'd need to do the same thing for west. Note that if the domain name is www.example.com or example.com, it cannot be east.example.com or west.example.com

Related

remove directory after main url using htaccess

With the following url http://www.example.com/de/here/ I want to remove the "de" directory (or whatever may be in front of the "here" directory, if anything even is in front of it) so a user is directed to http://www.example.com/here/ instead, which is a directory that does actually exist.
The url could even be something like http://www.example.com/it/here/ or any other combination of 2 letters.
The url could also just be http://www.example.com/here/ in which case I don't want anything removed at all.
I have searched for a solution here but cant seem to make anything work correctly so any help would be much appreciated.
You can use this kind of htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/(.*)$ $1 [L,R=301]
Example of redirections caused by this code :
http://www.example.com/de/foo/ => http://www.example.com/foo/
http://www.example.com/de/ => http://www.example.com/
http://www.example.com/it/bar/ => http://www.example.com/bar/
http://www.example.com/FR/baz/ => http://www.example.com/baz/
Please note you won't be able to access the language (de, it, fr...) anymore.
Another point, be careful with this kind of url (the redirection will be executed twice) :
http://www.example.com/de/go/ => http://www.example.com/go/
http://www.example.com/go/ => http://www.example.com/
EDIT
Now I've got more details, here is an htaccess you can you to remove the language for specified folders :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/here/(.*)$ here/$1 [L,R=301]
RewriteRule ^[A-Za-z]{2}/anotherfolder/(.*)$ anotherfolder/$1 [L,R=301]

.htaccess rewrite rule - custom structure

Is it possible to create a .htaccess rule which will take the middle of a URL structure, but resume the normal REQUEST_URL (Sorry for my terrible explanation)
Take this URL for example
/boats/283/manage/water
Now let's say I'm keeping the hierarchy standardisation as per the URL structure, minus the ID (ID in this case is 287) - so the actual script location is /boats/manage/water(.php)
But obviously I don't have to have a manual rule for each page, as that will get tedious.
eg (What I want to avoid per page).
RewriteRule ^boats/(\d+)/manage/water$ ./boats/manage/water.php?id=$1
RewriteRule ^boats/(\d+)/manage/bacon$ ./boats/manage/bacon.php?id=$1
I have no doubt I could find something relevant in Google, but I just can't quite come up with the proper keywords..
Any help/push in the right direction is much appreciated :)
You can try:
# group out the first path, the ID, then the rest
RewriteCond %{REQUEST_URI} ^/([^/]+)/(\d+)/(.*)$
# pre-check that the destination php file actually exists
RewriteCond %{DOCUMENT_ROOT}/%1/%3.php -f
# rewrite
RewriteRule ^ /%1/%3.php?id=%2 [L]

How to 301 redirect pages "up" a page

I'm a newbie and I'm trying to figure out the proper 301 redirect for the following pages. I hope I'm being clear here :) In my .htaccess file, I want to redirect pages "up" one pages without having to do every page individually.
My original pages looked like the following:
www.doctors.com/skin/california/best-skin-doctors-california/
www.doctors.com/skin/california/best-skin-doctors-california/?page=1
www.doctors.com/skin/california/best-skin-doctors-california/?page=2
....etc. ....up to like /?page=33
and more categories and states, like:
www.doctors.com/heart/new-york/best-heart-doctors-new-york/
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=1
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=2
...etc. .....again up to like /?page=24
I've since changed the page structure to eliminate the long URLs...like this:
www.doctors.com/skin/california/
www.doctors.com/skin/california/?page=1
www.doctors.com/skin/california/?page=2
etc.....and similarly....
www.doctors.com/heart/new-york/
www.doctors.com/heart/new-york/?page=1
www.doctors.com/heart/new-york/?page=2
etc.
How can I "bulk" redirect the original pages with the long URLs to the newer, shortened version in my .htaccess file? Thank you very much for your time and consideration!
Using mod_alias, you can simply add this in the .htaccess file in your document root:
RedirectMatch 301 ^/([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/
But if you need further restrictions on how the redirect works, you can use Apache's mod_rewrite module. Taking a look at the RewriteCond directive, you can impose conditions on a rule and put everything in .htaccess. The main rule will look very similar to mod_alias' RedirectMatch. Example:
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
In both cases, the query string (the page=3 part) is simply appended to the new target. Looking over the different things you can do with RewriteCond, say if you wanted to exclude this rule when requests are made for something like /images/ or /themes/:
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/themes/
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
So, if the request doesn't start with /images/ and the request doesn't start with /themes/, then apply the rule. This example would make it so a request for http://host.com/themes/subSilver/magic-icons/ don't get redirected to http://host.com/themes/subSilver/.

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

Htaccess rewrite if address is subfolder?

I have the following URLs:
www.mydomain.com/client
www.mydomain.com/client/index.php
www.mydomain.com/client/index.php?a=b
www.mydomain.com/client/index.php?a=b&b=c
The following two htaccess files exist:
www.mydomain.com/.htaccess
www.mydomain.com/client/.htaccess
I want to edit "www.mydomain.com/client/.htaccess" so that if you go to www.mydomain.com/client, that it redirects the user to mydomain.com/client/clientarea.php. In other words, 1 and 2 must redirect to mydomain.com/client/clientarea.php, but 3 and 4 must not.
How do I do that?
Try this:
RewriteEngine on
RewriteBase /
RewriteRule ^client$ /clientarea.php [L]
RewriteRule ^client/index.php$ /clientarea.php [L]
htaccess files are parsed in the order they're discovered, so the top level one (mydomain.com/.htaccess) will be parsed and executed before Apache even considers looking down in the .../client sub-directory. As such, you'd have to modify your rewrite rule to check if the request contains a subdirectory, and NOT process it if one's found.

Resources