rewriterule for multiple 301 redirects - .htaccess

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]

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

htaccess block pages based on query string for crawlers

I would like to block some specific pages from being indexed / accessed by Google. This pages have a GET parameter in common and I would like to redirect bots to the equivalent page without the GET parameter.
Example - page to block for crawlers:
mydomain.com/my-page/?module=aaa
Should be blocked based on the presence of module= and redirected permanently to
mydomain.com/my-page/
I know that canonical can spare me the trouble of doing this but the problem is that those urls are already in the Google Index and I'd like to accelerate their removal. I have already added a noindex tag one month ago and I still see results in google search. It is also affecting my crawl credit.
What I wanted to try out is the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} module=
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|Baiduspider) [NC]
Is this correct?
What should I add for the final redirection?
It's a tricky thing to do so before implementing anything I'd like to make sure it's the right thing to do.
Thanks
That would be:
RewriteEngine On
RewriteCond %{QUERY_STRING} module= [NC]
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|Baiduspider) [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
Last ? in %{REQUEST_URI}? will remove previous query string.

htaccess sport links redirects

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]

301 redirect get pagination

I'm trying to 301 redirect a paginated blog list from an old site onto a new url.
I think I'm getting pretty close with the RewriteRule but I'm not quite there yet, this is what I have:
RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^(blog)?$ http://www.newdomain.com/news/page/$1? [R=301,L]
Using this rule if I go to
http://www.olddomain.com/blog?page=1
I currently get redirected to
http://www.newdomain.com/news/page/blog
I would like to be sent to
http://www.newdomain.com/news/page/1
I'm sure its just something small and simple that I'm missing.
Edit
Expanding on the solution below, I've added tags/category support to the rewrite rule using $1.
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/tag/([^/\.]+)?$ http://www.newdomain.com/news/tag/$1/page/%1? [R=301,L,NC]
Few minor mistakes in your code.
You need to capture page parameter's value from query string first
Then use that capture value using % instead of $1
No need to capture blog since you don't need it.
Change your code with:
RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/?$ http://www.newdomain.com/news/page/%1? [R=301,L,NC]

RewriteRule does not work, while the rest do

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.
http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/
all are actually using http://kn3rdmeister.com/blog.php.
The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.
the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]
The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.
The script works, but the redirect doesn't. Going directly to
http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
will load the right content. However, going to
http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
doesn't.
Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]
But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.
Whilst webarto comments are good advice, your problem is a missing [:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$
not
^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$

Resources