htaccess apache rewrite rule - apache

I had a script that ran weather update urls in /weather which is now deleted
These types of urls are 404'ed and need to be redirected to /index.php as I show below
www.atlanticcity.com/weather/index.php?config=&forecast=pass&pass=local_radar&dpp=1&radar_icao=karx&hwvradartype=comp&place=wisconsin+rapids&state=wi&zipcode=&country=us&county=55141&zone=WIZ035
This I tried and doesnt work....
RewriteRule ^weather/(.*) http://www.atlanticcity.com [R=301,L]

You could use a redirect rule instead of rewrite. This would have to go in the apache .conf file under the virtual hosts tag.
Redirect 302 /weather/.* http://www.atlanticcity.com/index.php

Related

convert nginx redirect to .htaccess apache (drupal 7)

I have to redirect url in .htaccess:
original: /mobile/blog/
redirect : /blog/
I tried:
RewriteCond %{REQUEST_URI} ^/mobile/blog/
RewriteRule ^mobile/blog/(.*)$ /blog/$1 [R=301,L]
Without success.
I aslo have these nginx redirect that need to be in .htaccess.
rewrite ^/items/(.+)/(.*) /search/$2 permanent;
rewrite ^/items/(.*) /search/$1 permanent;
rewrite ^/topic/onlyon $scheme://$host/tags/onlyon permanent;
Any help and documentation is appreciated.
Since Drupal sometimes updates the .htaccess file and Drupal often maintains a set of redirect internally it often makes sense to handle redirects using the Redirect module.
That said, if you need or want to do redirects in the .htaccess you can do that just fine. The first redirect you can do without rewrite, you can use Apache's redirect directive:
Redirect permanent /oldlocation /newlocation
To move everything in a directory using rewrite you can use something like this:
RewriteRule ^items(/.*)?$ /search/$1
See also: Apache rewrite rule for whole directory

301 htaccess redirect from old path to new path

I want to the following redirect 301 to happen on .htaccess file for my site.
From:
www.mysite.com/view/2133/page.html
To:
www.mysite.com/node/2133
The 2133 will be a dynamic number like a * (eg www.mysite.com/view/*/page.html)so it will work for all my other pages.
Try:
RewriteEngine On
RewriteRule view/([0-9+]) /node/$1 [R=301,L]
For an explanation see: URL Aliasing, Redirection, Rewriting and Reverse Proxying using Apache HTTPD

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

How to redirect to subdomain but then allow normal use of site

So I have my site, www.domain.com.
For a week or so I want to direct all traffic going direct to the site to subdomain.domain.com, a little promo page about an upcoming feature. I want visitors to then be able to continue to the site as normal though after they've read it, so a continue to www.domain.com/index.php link.
How can I do that with in the htaccess file? Everything I've tried so far messes up when clicking the continue link.
Thanks
with .htaccess you could use a 302 temporary redirect, but it would be for a whole sub folder as far as I know.
Another way would be to redirect with JS/server site language to the subdomain, create a cookie, then redirect back to www.domain.com/index.php .
the 302 redirect is explained here: How do I redirect my site using a .htaccess file?
You would need to have a .htaccess for the root folder point to your subdomain
Note that this is only possible if you enable mod_proxy in the Apache config of domain.com otherwise URL will change after redirect.
Enable mod_proxy, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^index\.php$ http://subdomain.domain.com/ [L,NC,P]

301 redirect not working

I made a subdomain, and I am trying to redirect a page from the original domain to the subdomain, with the same dir structure.
My original page is this:
http://www.comehike.com/outdoors/alaska_hiking.php
I put this rule into the .htaaccess file under comehike.com/outdoors/ directory:
RewriteRule ^outdoors/alaska_hiking.php http://hiking.comehike.com/outdoors/alaska_hiking.php [R,L]
But as you can see from visiting the original url, it doesn't redirect. Any idea why?
Thanks!
I use apache server.
That RewriteRule line works for me when I stick it in my .htaccess file, are you sure you have RewriteEngine On?