htaccess sport links redirects - .htaccess

Need to redirect set of hundred or so links from one domain to another. This is my current code (not working):
RewriteCond %{HTTP_HOST} ^www.onedomain.info/$1/staticword($2.*) [nc]
RewriteRule (.*) http://otherdomain.com/$1/staticword($2.*) [R=301,L]
Redirect domains themsleves is a no-brainer and that's correct I think, then I too think that $1 is correctly - cuz $1 is a variable for 12 different words for sport categories (like soccer or hockey), sometimes there is one word, sometimes the other (but ofc it should be the same, so this is why I have that $1 there - correct me if I am wrong but this could work I think...).
Problem is that after that there is one static word which is not changing (is same all the time in every link - it's something like "watch"...) BUT after that word there can be absolutely ANYTHING which I tried to solve by ($2.*) but it's wrong for some reason.
Can you help please? Thanks!

RewriteEngine On
# HTTP_HOST if to match domain names only
RewriteCond %{HTTP_HOST} ^www\.onedomain\.info$ [NC]
# On the left hand we match the rest and on the right we redirect
RewriteRule ^(.*)/(staticword.*)/?$ http://otherdomain.com/$1/$2 [R=302,L]
Note that I am using 302, because you want to test it first before change it to 301 so your browser does not get cached with it until you are sure it's working as you want it to.
So given your example http://onedomain.info/soccer/watchfe27789-mexico-vs-trinidad-and-tobago-gold-cu‌​p, the rule would be like this:
RewriteEngine On
# HTTP_HOST if to match domain names only
RewriteCond %{HTTP_HOST} ^www\.onedomain\.info$ [NC]
# On the left hand we match the rest and on the right we redirect
RewriteRule ^(.*)/(watchfe27789.*)/?$ http://otherdomain.com/$1/$2 [R=302,L]

Related

HTACCESS 301 Redirect urls into two separate directories

I am trying to figure out how to redirect certain http urls from the same directory into two separate https directories with urls currently ending with forward slash to urls ending without forward slash. I have a school directory site with a main page for schools, a page for each state, and a page for each school profile listed at the site. Each state has a listing of schools and when clicked on a school, it will take you to the school profile page. If state has a lot of schools, they are shown in multiple pages with the state name followed by /2, /3, etc. representing page numbers
So the old structure looks like this:
http://www.example.com/schools/
http://www.example.com/schools/california/
http://www.example.com/schools/california/2/
http://www.example.com/schools/california/3/
http://www.example.com/schools/new-york/
http://www.example.com/schools/some-great-high-school/
After migrating to laravel, I have added ssl, removed the forward slash from the url, and separated the states listings from schools profile pages into two directories as follows:
https://www.example.com/schools
https://www.example.com/schools/california
https://www.example.com/schools/california/2
https://www.example.com/schools/california/3
https://www.example.com/schools/new-york
https://www.example.com/school/some-great-high-school
I have tried the following and it seems to work
RewriteEngine On
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The above code takes care of redirecting non www to www and at the same time redirects http to https. Then I have the following:
RewriteRule ^schools/alaska/$ /schools/alaska [R=301,L]
RewriteRule ^schools/alabama/$ /schools/alabama [R=301,L]
RewriteRule ^schools/arkansas/$ /schools/arkansas [R=301,L]
.
.
.
.
RewriteRule ^schools/wyoming/$ /schools/wyoming [R=301,L]
RewriteRule ^schools/(.*)/$ /school/$1 [R=301,L]
RewriteRule ^schools/(.*)/0+([0-9]+)/?$ /schools/$1/$2 [R=301,L]
RewriteRule ^schools/$ /schools [R=301,L]
This works but it seems very inefficient, and there has to be a better way to do this. For one, with the above method I have to list each state individually on 50 lines, then it does two 301's. For example: When someone clicks on one of my old backlinks http://www.example.com/california/ it does the following:
301 https://www.example.com/california/
301 https://www.example.com/california
There has to be a better way to do this. Since all states only have alphabets and either have one word (i.e. california) or two words separated by a dash (i.e. new-york), and all school profiles have at least three words each separated by a dash (i.e. some-great-high-school), I think there's got to be a way to build that into some query and redirect that into the https version with one try. But I have exhausted myself trying to figure it out without any luck. I tried the following and a few variations:
RewriteRule ^schools/([a-z])/$ https://www.example.com/schools/$1 [R=301,L]
RewriteRule ^schools/([a-z])-([a-z])/$ https://www.example.com/schools/$1 [R=301,L]
RewriteRule ^schools/(*.)/$ https://www.example.com/school/$1 [R=301,L]
Only the third one works, but it redirects everything including states into the "school" directory.
I hope I have illustrated my request well enough and thank you in advance for your help.
Thanks and regards,
Mike

rewriterule for multiple 301 redirects

I've tried looking my question up, but the closest answers I've found didn't work--especially since I'm VERY new to editing .htaccess files.
I have a site that has been programmed to dynamically generate copies of a page to fit a location. For instance, example.com/help/work/ was set up to make about 100 duplicates that look like this: example.com/help/work/?city=Washington&state=DC with the city and state dynamically changing with each page.There are tons of these variations and I want to 301 redirect all the pages with a city and state parameter so they point to the original page (example.com/help/work/).
After some research, I was able to find a RewriteRule that helped me do this on a page by page basis, but only with the homepage:
RewriteCond %{QUERY_STRING} ^city=Philadelphia&state=PA$
RewriteRule ^$ http://example.com/? [R=301,L]
With all that said, I have a two part question:
Is there a way to write this so that it targets subdirectory pages? (I could only get it to do the index)
Is there a way I can use a wildcard like (.*) in a single RewriteRule so example.com/help/work/?city=Washington&state=DC and all its city/state variations point to the original page (example.com/help/work/)?
I'm a bit confused on your request. It appears you want to point every city and state to this single page. http://example.com/help/work/ See if this is what you're looking for.
RewriteCond %{QUERY_STRING} city=.+&state=.+
RewriteRule ^([^/]+)/([^/]+)/?$ http://example.com/$1/$2/? [R=301,L]
Yes, You can use a regex capture group in Rewrite rule that captures the request_uri dynmically .
Like this
RewriteCond %{QUERY_STRING} ^city=.+&state=.+$
RewriteRule ^(.*)$ http://example.com/$1? [R=301,L]

friend url on wildcard subdomains htacces, not working

i have wildcard subdomains sets already and works fine, now i wish have friends url for the content in thats subdomains, the structure of my site is if the user type subdomain.maindomain.com and the .htaccess redirect to
blogs/index.php?user=subdomain
where blogs/index.php receive the param and show the correct content
now i try to make the url function like this
subdomain.maindoamin.com/24/title-of-content
and then .htaccess must result
blogs/index.php?id_content=24&title=title-of-content
i have the next .htaccess
Options +FollowSymLinks
#this force to server the content always without www.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301]
#this is to pass the subdomain like param and show the right content of the user
RewriteCond %{HTTP_HOST} !^www\.misite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.misite\.com
RewriteRule ^(.*)$ blogs/index.php?url=%1 [QSA,L]
#the next line i can't make work to make nice url
RewriteRule ^/(.*)/(.*)$ blogs/index.php?idP=$1&name=$2 [L]
not working because when i make in index.php
echo $_SERVER['REQUEST_URI'];
don't show idP=24 show /24/title-of-content and i need $_GET(idP)
i really apreciate some light on this stuff i am not expert on htaccess, thanks in advance to everybody.
There are two problems:
The first argument of RewriteRule matches against everything after the slash of the directory .htaccess is in, and before the query string. If .htaccess is in your www-root, and you get the url http://www.example.com/shiny/unicorns.php?are=shiny, you match against shiny/unicorns.php. It will never start with a slash, so ^/ will never match.
Rules are executed in order. If you go to http://sub.example.com/10/unicorns, the second rule will match first and rewrite the request to /blogs/index.php?url=10/unicorns. If you removed the leading slash the third rule would match, but normally you wouldn't want that. You want to have the third rule only match
You want to move the third rule up so it is the second rule. You want to make it more specific to only match with subdomains. You also know the first part contains only numbers, so use that knowledge to prevent blogs/index.php from matching your now second rule. You also need to prevent blogs/index.php from matching the now third rule to prevent it from matching itself. Last but not least I removed [L] from the now second rule, since the third rule will match anyway.
#the next line i can't make work to make nice url
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^([0-9]+)/([^/]+)$ blogs/index.php?idP=$1&name=$2
#this is to pass the subdomain like param and show the right content of the user
RewriteCond %{HTTP_HOST} !^www\.misite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.misite\.com
RewriteCond %{REQUEST_URI} !/blogs/index\.php
RewriteRule ^ blogs/index.php?url=%1 [QSA,L]

.htaccess: Using domain name from RewriteCond

I've been googling the hell out of the problem I'm having and so far I have found nothing that works.
What I have is a Drupal multi-site installation that I'm trying to setup redirects for mobile on. To start I have 10 domains that have only one common string, www. What I want is to check if the device viewing the site is mobile (I'm using AMF to successfully do this) and make sure that the visitor is not already on the mobile version which will be m.domain.tld. If all is good then I need to redirect the user to the mobile subdomain.
Doing this per domain works using the following:
RewriteCond %{ENV:AMF_DEVICE_IS_MOBILE} ^true$
RewriteCond %{ENV:AMF_DEVICE_IS_TABLET} !^true$
RewriteCond %{HTTP_HOST} !^[m.]+mysite.com$
RewriteRule (.*) http://m.mysite.com/$1 [R=301,L]
I do not, however, want to have 10 of those blocks (1 for each domain) nor do I want to have to create another block every time we create a new website.
I have read that you can use () in the RewriteCond patterns to make a var out of the matching pattern. Unfortunately I don't think I'm making my pattern properly or something because when I try the following it redirects to m. followed by whatever the request uri is.
I'm leaving out the AMF conditions for brevity.
RewriteCond %{HTTP_HOST} !^[m.]+([^.]\.[com|net|org])$
RewriteRule (.*) http://m.%1/$1 [R=301,L]
From what I read, the %1 should match my pattern from the RewriteCond but it isn't matching anything. That makes me think my pattern is wrong. I've tried changing ([^.].[com|net|org]) to (.*) but get the same issue where it just redirects to m..
Am I right in assuming my pattern is wrong and if so what should I be using? Is what I'm trying to do even possible?
To be clear, my domains are in the form of:
www.domain.com
www.anotherdomain.net
somedomain.org
www.maybeanotherdomain.com
In my examples I have the flag R=301 but in testing I'm leaving that out and have to clear a lot of data from the browser each time I test to make sure I'm picking up the new rule.
Thanks in advance for any help!
You can't backreference a not match (!), but you can separate the condition into 2 matches against the %{HTTP_HOST} variable:
RewriteCond %{HTTP_HOST} !^m\.
RewriteCond %{HTTP_HOST} ([^\.]+)\.(com|net|org)$ [NC]
RewriteRule (.*) http://m.%1.%2/$1 [R=301,L]
You may need to tailor the ([^\.]+)\. bit to suit your needs, this expression will only match the "domain" part of "www.domain.com".

htaccess subdomain rewrite

It is many topics here about subdomains but no one can help me...
I use htacces to set subdomain to folder
So if we put http://en.example.com/something
I use something like this..
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://example.com/%1/$1 [NC]
This works fine but adress in bar is changed to http://example.com/en/something but I want keep http://en.example.com/something
so I tried this
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^([^.]+)\.example\.com(.*) /$1/$2
or just
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 [NC]
but this doesn't work. Any solution or ideas ?
One solution is use language there (http://example.com/en/something) where I rewrite it but after If I work on subdirectories I get something like http://example.com/subdirectory/en/something - terrible.
Maybe I like http://example.com/en/subdirectory/something bud how proceed this...
And also on some private servers first one case send me to "maybe" default domain, so it is not working for me. (maybe this is some server condition or settings)
I know this is a month late, but maybe this will still be useful for somebody. A few things here:
Regarding your first RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://example.com/%1/$1 [NC]
As it seems you already discovered, rewriting to another URL will also redirect the user's browser to that new URL, even if it's at your own domain. To keep it hidden, you have to rewrite to a file path on the server (like you do with your next two rules).
Regarding your second RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^([^.]+)\.example\.com(.*) /$1/$2
The problem there is that you can't match the domain name in the RewriteRule, only the URL path. If your URL is www.example.com/something/somethingelse, the string you're trying to match is just something/somethingelse. In other words, it excludes www.example.com/, so this RewriteRule pattern you have will never match the domain name because the pattern isn't even being tested against that part of the URL, but you included the domain name in the pattern, causing the match to fail.
Regarding your third RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 [NC]
This looks like it should work, so I can't say for sure why it isn't without knowing more about how your files are organized on the server and so forth. Let's say you have all of the website's files in /home/somebody/public_html/. In order for the RewriteRule to work as it is right now, you would need to have an en subdirectory in public_html. So, if somebody went to en.example.com/something, the RewriteRule would cause Apache to serve the file at /home/somebody/public_html/en/something. My guess why it's not working for you is that you might have the subdomain pointing somewhere other than public_html (assuming you actually had the website files organized like in my example). Remember that what you're rewriting to (/$1/$2 in this case) is a file path on the server, not a URL to your website.
I hope that helps! You may have already solved this by now, but even if you have, I'm hoping other people will still find this useful.

Resources