.htaccess redirect subdomain to a subdirectory - .htaccess

I am developing a website in which user registers with some of their personal data. Each of them get a unique URL to their profile (Please see the below example).
Is there any way to get a unique url like a subdomain to each users with htaccess rewrite rule ?
Example
For John - john.example.com > example.com/user.php?username=john
For Emma - emma.example.com > example.com/user.php?username=emma
I have some other redirect rules for other pages like login, registration etc
Is there any way ?
Live example - https://john.sarahah.com, https://emma.sarahah.com

Rewrite rules use regular expressions, so you have full power:
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond "${lowercase:%{HTTP_HOST}}" "^([^.]+)\.example\.com$"
RewriteRule "^(.*)" "https://example.com/user.php?username=%1;path=$1" [R,L]
This is almost the first example at https://httpd.apache.org/docs/2.4/rewrite/vhosts.html

Related

htaccess | mod_rewrite | redirect to URL domain parameter

I am migrating an ASP web site into WordPress. Currently, I am fixing the 404 errors, from the old website to the new but I have find a URL that looks like the following
http://www.mysite.com/www.customer-web-site.com
and I like to redirect this domain to
http://www.customer-web-site.com
currently I have use the following rewrite rule, but with no luck:
RewriteRule ^(www\.([^\.]*)\.([a-z]{2,3})) http://$1 [R=301,L]
but this, redirect me to
http://index.php/
Can somebody help me ?
Kind regards
And what about $1 in the rewrite rule? Does changing it to $0 fixes this?
$1 to $9 provide access to the grouped parts (in parentheses) of the
pattern, from the RewriteRule which is subject to the current set of
RewriteCond conditions. $0 provides access to the whole string matched
by that pattern.
Apache mod_rewire doc

301 Redirect in htaccess. Matching Page IDs

I would like some help creating an htaccess 301 redirect for the below type of url.
In total there's around 500 or so products but rather than write a redirect for every url, which would be very bulky and time consuming, I'm hoping there's an easier way that I haven't yet found to create a kind of regular expression match?
OLD: http://www.example.co.uk/test-product-name-slug/prod_233.html
NEW: http://www.example.co.uk/test-product-name-slug-233.html
The new URL can be accessed by browsing to ..... example.co.uk/-223.html ...... which then rewrites to ..... example.co.uk/test-product-name-slug-233.html
So it would appear I need a way of detecting if the incoming visitor is coming to a url that cotains prod_id and redirecting to -id
I hope that all makes sense.
Hopefully this is what you're looking for. It only matches 1 directory in
/some-product_name-random/prod_9393.html => /some-product_name-random-9393.html
.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-A-_]+)/prod_([0-9]+)\.html$ /$1-$2.html [R=301,L,QSA]
Regex and Parameters Explained
([a-zA-A-_]+) matches product name $1
prod_([0-9]+) matches product id $2
[R=301] 301 permanent redirect
[L] stop .htaccess script (may be removed, but usually good practice for specific rules when using multiple rules for different scenarios)
[QSA] keep query string domain.com/somepath/page.html?querystring=value&otherstuff (?...)

htaccess 301 redirect all website pages with query to /

In the past website was full of pages with different basis. After redisign and with new concept the website consists of only 1 page. So I need to redirect all old pages to /.
Pages for example:
http://domain.com/catalog/index.html?c_id=145
http://domain.com/catalog/?c_id=116
http://domain.com/news/read.html?id=174
and so on.
In htaccess I've made several rules like this:
RewriteRule ^catalog(.*)$ http://domain.com/ [L,R=301]
But this rule don't give a full control, and the result is not satisfactory:
http://domain.com/?c_id=145
How can I get rid of this? I need to redirect all such links to http://domain.com without any additions. I think the {QUERY_STRING} can help, but I don't know how to do this correct.
Any help would be appreciated. Thanks!
You can add a ? at the end to create a blank query string:
# append "?" here ----v
RewriteRule ^catalog(.*)$ http://domain.com/? [L,R=301]

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/mod_rewrite: West <--- Main Site ---> East

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

Resources