htaccess rewrite being overwritten - .htaccess

I have the following htaccess file in the root of my site to redirect a directory 'MyDirectory' to another URL (to stop google indexing both sites)
RewriteCond %{HTTP_HOST} ^myurl\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myurl\.co\.uk$
RewriteRule ^mydirectory\/(.*)$$ "http\:\/\/www\.redirectURL\.com/" [R=301,NC]
I then have another htaccess file in the 'MyDirectory' directory above which handles the URL and query string in a user friendly way:
RewriteRule ^Answers/(.+)$ Answers.php?articleName=$1 [QSA]
However, when i have the ReWriteRule ^Answers... section in my other htaccess it stops the redirect in the root of my site from working.
Any suggestions why the ^Answers/... rewrite is overwritting my redirect to www.redirectURL.com.
Thanks

.htaccess is per directory directive and for any URI path it current directory's .htaccess is processed first. If no .htaccess is found then it starts going 1 level up the directory tree until it finds one. It processes DocumentRoot/.htaccess in the end.
In your case since you have a /MyDirectory/.htaccess therefore all the directives are read from that file only thus overriding all the rules of DocumentRoot/.htaccess.
However you can add this line after RewriteEngine On in /MyDirectory/.htaccess
RewriteOptions inherit
This will process parent .htaccess after completing current one.

Related

Redirecting “.htaccess” with same folder & file name

A while ago I rebuilt a site that was on a CMS. As expected the Google search console regularly comes up with URLs that generate 404 crawling errors which I would promptly redirect to their new equivalents using a 301 on a .htaccess file.
The issue is that I came across the URL of an old page which looked like this:
http://www.example.com/example
The new page is now called http://www.example.com/example.php.
There is, however, a folder called example too so if I add a redirect like this:
Redirect 301 /example http://www.example.com/example.php
everything in that folder also gets redirected, so a URL called http://www.example.com/example/product.php would now show up as http://www.example.com/example.php/product.php.
Is there a way to redirect the old URL successfully without having to rename the folder example?
Please clear your browser cache and then put the following code at main root .htaccess file :
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+example [NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [R=302,L]
The code above will stop directoryslash and make it off so no file without slash goes to be directory as default and then check if example exists in QUERY_STRING and the trick comes here in this line RewriteCond %{REQUEST_FILENAME}\.php -f,it will check if adding .php to this string , is that really file in directory or not ? and already we have example.php in root so , it will go to last line to redirect any string that passed the rules above to itself.php and that what you want exactly.
The above code will catch any example in only root directory with example only but if wanna inherit other sub-folders or apply this rule to entire site so , whenever there is string without extension the priority will be for the file , put the following code at your main root .htaccess file :
DirectorySlash Off
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [R=302,L]
After testing this code , if it is Ok , change 302 to 301 to be permanent redirection

.htaccess rewrite from subdirectory

Any help with this would really be appreciated.
I am setting up 301 redirects in a .htaccess file to redirect hundreds of old urls to the latest live pages.
My .htaccess file is currently located in the root of the domain and looks something like this:
RewriteCond %{QUERY_STRING} ^pName=product-one$ [OR]
RewriteCond %{QUERY_STRING} ^pName=product-two$ [OR]
RewriteCond %{QUERY_STRING} ^pName=completely-different-name$
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
While researching for answers on how to bulk redirect to one url using .htaccess, I read that it would wise to change the location of the .htaccess file and place it in the specific directories to which it apply's, so requests to other directories won't even these rules.
So I have placed the .htaccess file in the specific sub directory, which is catalog, but the redirect no longer works. What am I missing here? I want the rules for the urls to be redirected to be placed in the .htaccess file inside the catalog folder so all those rewrite rules wont be loaded each time the root .htaccess is loaded.
I would suggest you change your redirect 301 line to contain the path directly to you catalog folder of OSCommerce.
So instead of using
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
use e.g.:
Redirect 301 /website/catalog http://www.mydomain.com/page2.htm
or
Redirect 301 /catalog http://www.mydomain.com/page2.htm
Option 1) is if your catalog is located at http://youdDomain.com/website/catalog/ .
If it is located at http://youdDomain.com/website/aaa/catalog/ you would use:
Redirect 301 /website/aaa/catalog http://www.mydomain.com/page2.htm
I tested this in my own webserver - hope it helps.
When you put the .htaccess in a subdirectory, you must adjust the RewriteRule pattern accordingly. mod_rewrite strips the directory prefix, where the .htaccess file is located, see RewriteRule - What is matched?.
You must change the rule to
RewriteRule ^product_info.php$ ...
If you have lots of URL paths to rewrite, you might also want to look into RewriteMap.

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

subdomain including htaccess redirect

I have a subdomain code.domain.com that redirects to domain.com/code
I want to redirect for example: code.domain.com/hello/world.html to domain.com/code/?path=hello/world.html
This is what I have so far:
RewriteEngine ON
RewriteRule ^code/(.+)$ http://domain.com/code/?path=$1
But I am not sure what is wrong with it, it doesn't seem to work.
Thanks.
Use these rules in the root directory htaccess file (not the code directory) (you can also point the code.domain.com to the root directory instead if you want) :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^code.domain.com$
RewriteRule ^(.+)$ /code/?path=$1

htaccess redirect remove /app/webroot in cakephp

I have an htaccess file that I want to redirect the all pages to the another folder. I am using cakephp for the site, except for this folder which I want to redirect.
"/app/webroot/" is added to the new directory so the url is /app/webroot/new/ instead of just new.
I am placing this htaccess file (below) in the "new" directory and want anything at /app/webroot/new/ to redirect to /new/ and remove the /app/webroot/ This folder is independent of cakephp and thus does not need to be processed by cake.
The code below loops and I am not sure why.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /new/
# If your concerned about direct access to a particular page without the sub-dir
# you will want to add something like this
RewriteCond %{REQUEST_URI} !^/new
RewriteRule (.*) /new/$1 [R=301,L,NC]
You should NOT place the .htaccess file in /new/ but in /app/webroot/new/.

Resources