htaccess subdirectory URLS to root - .htaccess

I want to redirect all files in a subdirectory to their new root URL.
Old URLS are
www.domain.com/subdirectory/filename-here.html
New location is
www.domain.com/filename.here.html
Old URLs are listed in Google already and need some sort of redirect for these.
I'm sure htaccess is the way to go here, but unsure of the solution.

RewriteEngine on
RewriteRule %{REQUEST_URI} ^/subdirectory/.*
RewriteRule subdirectory/(.*)$ /$1 [R=301,L]
Tested on here. If it would not work - add shielding to [^/] in this way: [^\/]

Related

301 Redirect root without cascading new root directory to all other 301 redirects

I have successfully redirected the root of my site to a sub-directory in another site:
#Redirect Root to subdirectory on example.com
RewriteEngine On
RewriteRule ^$ http://example.com/subdirectory [L]
I'm running into trouble with all of my other redirects. The sub-directory of the new root (/subdirectory) is getting inserted into my redirects before each path, which is resulting in broken redirects.
Example:
Redirect 301 /contact-us/ https://www.example.com/company/contact.shtml
tries to point to
https://www.example.com/subdirectorycompany/contact.shtml
In a nutshell, I can either get the root working, or I can get the redirects working -- but not both. Can anyone please lend some insight?
I have read a bunch of posts, which is how I have gotten as far as I did, but I could use some help, please and thank you.
Don't mix Redirect directive and RewriteRule as they are from different Apache modules and are invoked at different times.
It is better to have all your rules like this:
RewriteEngine On
# all specific 301 redirects go here
RewriteRule ^contact-us/?$ https://example.com/company/contact.shtml [L,NC,R=301]
#Redirect Root to subdirectory on example.com
RewriteRule ^$ http://example.com/subdirectory [L,R=301]

.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 URL rewrite rule for everything under a folder

I have looked but can't find anything that works. I have an old site that we have updated. They had everything under a folder called site under the root. Now all the customers who have this bookmarked I would like to redirect them, regardless of what is after the folder site (subfolders, files), to the main page of the new site instead of page not found on our new WordPress install. Any help appreciated.
Old URL: http://www.oldsite.com/site/.... to new URL http://www.newsite.com
I have tried this to no avail
Rewrite Rule ^site/(.*)$ http://www.newsite.com
Thanks.
try:
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite.com$ [NC]
RewriteRule ^site/(.*)$ http://www.newsite.com/$1 [R=301,L]
This redirects something like http://www.oldsite.com/site/some-page.html to http://www.newsite.com/some-page.html (the matching bit of the URI after /site/ gets carried over in the 301 redirect), but if you want to redirect everything for /site/ to the index root of newsite, replace the target in the RewriteRule to http://www.newsite.com/ (remove the $1 bit).
EDIT:
I actually write it wrong above. It is actually the same domain name. The question should read old URL mysite.com/site.... everything under this folder to just redirect to mysite.com
Then what you want is:
RewriteEngine On
RewriteRule ^site/(.*)$ /$1 [R=301,L]
Or alternatively with mod_alias:
RedirectMatch 301 ^/site/(.*)$ /$1
Looks like all you need is this simple 1 liner rule:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^site/ / [R=301,L,NC]

.htaccess redirect for images from old folder to new folder

I have just moved from Drupal + Wordpress to a site completely built in WordPress.
Duly I have a set of images where the files no longer exist and need to try and keep all the images in the one folder (if possible). Duly I need to send requests for any gif|png|jpg that are for http://www.domain.com/blog/wp-content/uploads/ to http://www.domain.com/wp-content/uploads.
If anyone could help would be appreciated - my .htaccess aint what it once was. Thanks in advance
If you google for "htaccess redirect", the top link is this:
http://www.htaccessredirect.net/
If you use the "301 Redirect Directory" section, you get this code:
//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) /wp-content/uploads/$1
As far as I know the target domain should be absolute, so the following might work:
//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) http://www.domain.com/wp-content/uploads/$1
Please try this rule in your .htaccess:
RewriteEngine On
RewriteRule ^/blog/wp-content/uploads/(.+)\.(png|gif|jpg)$ wp-content/uploads/$1.$2 [QSA,L]
Try this
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://www.domain.com/blog/wp-content/uploads [NC]
RewriteRule .* http://www.domain.com/wp-content/uploads [NC,L]
You could try and put this
RewriteEngine ON
RewriteRule ^/blog/wp-content/(.*)$ http://newdomain.com/wp-content/$1 [R=301,L]
What I have hated about all the re-write rules and redirect options for .htaccess files is they all rely on hardcoding the path (URI) and/or server for the redirect.
The point of the ".htaccess" files it it should be for the current directory! It could be referenced in a number of different ways, installed on different servers in different locations. So trying it down to a specific location for a simple directory rename is illogical.
The solution is to somehow incorporate the current URI (regardless or where the ".htaccess" location) into the result...
This is my current solution for location independent ".htaccess" redirect for a renamed sub-directory, and even I admit it is not perfect... BUT IT WORKS...
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^OLDdir/.*$ %{REQUEST_URI}::: [C]
RewriteRule ^(.*)/OLDdir/(.*)::: $1/NEWdir/$2 [R,L]

How do I fix old links after modifing .htaccess for Joomla installation in a subfolder?

I have my main Joomla installation in a subdirectory. I used to redirect users from www.mysite.com to www.mysite.com/subdir with a 301 so that the live site was entirely dislocated over there.
I don't actually like the fact that all the URL are preceded by the subdirectory /subdir/ (and I also think this is not very good for SEO) so I modified my .htaccess file like this:
RewriteEngine On
RewriteBase /
# Add trailing slash if path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.mysite.com/$1/ [R=301,L]
#Change http://yoursite.com to http://www.mysite.com (Optional)
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?(.*)$ http://www.mysite.com/$1 [R=301,L]
#Rewrites http://www.mysite.com/subdir to http://www.mysite.com/
RewriteRule ^(.*)$ subdir/$1 [L]
I also edited the configuration file for Joomla! so that now all the links in the site point (correctly) to www.main.com/theirquery and noto to www.main.com/subdir/theirquery
Now, however, all the old links (that have been posted to other webistes, for example) appears to be broken (404): how can I solve this?
I think I have to redirect (301) them to the new subdirectory-free address, that will be (another time) silently redirected with the htaccess I posted.
But I don't know how to do this!
Thank you in advance!
Can you try setting the $live_site parameter in configuration.php? You need to edit it directly rather than through the backend of Joomla.
You need to add this to your htaccess.
RewriteRule ^subdir/(.*)$ /$1 [R=301,NC,L]
This is not possible since the old links where not physical but stored in a database, so redirecting them wouldn't be possible without passing through the database again.

Resources