301 redirect not working - apache

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?

Related

Redirect with .htaccess to exact URL provided

So I want to redirect from old sites url to new one. Lets say example.com/en/some/stuff/foo/bar needs to be redirected to example.com/some/stuff.
Here is what I have at he moment:
Redirect 301 /en/some/stuff/foo/bar/ /some/stuff/
The problem is that I end up being redirected here example.com/some/stuff/foo/bar, but I need as I defined inside .htaccess example.com/some/stuff.
How to redirect properly to exact URL I have provided without anything extra.
You can use this redirect rule with regex in your site root .htaccess:
RewriteEngine On
RewriteRule ^en/(some/stuff)/.+ /$1 [L,NC,R=301]
Make sure to test it in a new browser to avoid old cache.

.htaccess Redirect all Subdomains to an URL

how can I redirect all of the subdomains to a URL
this is the scenario :
abc.xxx.com goes to abc.xxx.com/index.php?id=abc
www.xxx.com/abc or xxx.com/abc also goes to the same URL: abc.xxx.com/index.php?id=abc
and please bear in mind there is no /abc folder on the website
and the file index.php is the root file of the main domain
I want to keep the abc in the subdomain for SEO purpose.
Thanks in advance.
I think you must first check if URL does not contain "index.php?id=" to prevent infinite loop.
RewriteCond %{REQUEST_URI} !index\.php\?id= [NC]

How do I redirect an entire site to a new domain using .htaccess?

I have a site that is moving to a new URL, an exact copy of the original with the same pages and URL structure. I'm working with the old domain's .htaccess file to redirect to the new one, and have this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule (.*)$ http://newsite.ca/$1 [R=301,L]
The redirect works when I go to oldsite.com or www.oldsite.com works as expected, and redirects to newsite.ca
However, when I go to something like oldsite.com/blog/, no redirect happens. It just stays at the same URL.
I want to be able to redirect every page to the new site, not just the root.
oldsite.com/blog/ should redirect to newsite.ca/blog/
I need this for the whole site without having to go through and redirect every single page individually. Is this possible?
Figured it out-
I moved my code to the top of the .htaccess file.
The code wasn't being executed because there was a www to non-www before it. Apparently the code is run in order, and if a redirect is found, it will not run another kind of redirect following it.

.htaccess directory wildcard redirect

Please can someone tell me why this isn't working?
I am trying to redirect all files within the directory "Kent". In this example the page to be redirected is "ramsgate" and change the folder structure.
from:
/electrical-contractors/kent/ramsgate.php
to:
/electrical-contractors-ramsgate/
Using a wildcard redirect, why doesn't this work?
RedirectMatch 301 /electrical-contractors/kent/(.*).php /electrical-contractors-$1/
Have this rule inside /electrical-contractors/kent/.htaccess:
RewriteEngine On
RewriteRule ^(.+)\.php$ /electrical-contractors-$1/ [L,NC,R=301]

Ruby on Rails - .htacces RewriteRule to change image path

The situation
I use Carrierwave to upload images to the default upload/ directory. Works fine. When I want to view the images in the front end of my online web server, they don't show.
If I use my application in my local setup, the images show.
The only difference between my local and web server's setup is that on my server the application is run in subdirectory called "/app". Locally it's in the root directory.
What I have so far
I have this Redirect command in my .htaccess:
Redirect 301 /uploads/model/image/54/image.jpg /app/uploads/model/image/54/imgage.jpg
I want to formulate a general RewriteRule out of it so that all images which have the path "/uploads/model/image/" are found and redirected to "/app/uploads/model/image/".
How can I do that? I've tried several RewriteRules like:
RedirectMatch 301 /uploads/(.*) /app/uploads/
or:
Redirect 301 /uploads/model/image/(.*) /app/uploads/model/image//$1
or:
RewriteRule ^.*/app/uploads/(.*)$ http://%{HTTP_HOST}/uploads/$1 [L,R=301]
What I've read so far
I've read those entries on Stackoverflow:
How to change image path using htaccess?
Use Mod_Rewrite to Rewrite ROOT to Another Path
.htaccess rewrite to redirect root URL to subdirectory
I really have no idea why this happens would be very happy to get any hint, tip or link that could help me out of that.
You're close. Try:
RedirectMatch 301 ^/uploads/(.*)$ /app/uploads/$1
or
RewriteRule ^/?uploads/(.*)$ /app/uploads/$1 [L,R=301]