.htaccess redirect FROM subfolder to domain name - .htaccess

I had website content in a subfolder (http://mydomain.com/subfolder/index.php), now I copied everything over to the root folder (http://mydomain.com/index.php).
I would like to redirect all the visitors that have bookmarked the old page to the new content (at least to the new index.php) using .htaccess.
Is this correct:
RewriteEngine on
RewriteRule /subfolder/^(.*)$ http://mydomain.com [R=301,L]
?
And where would I place the .htaccess file, in the subfolder or the root folder?

Placing the following .htaccess in / (where your index.php is located) should do the trick:
RewriteEngine on
RewriteRule ^subfolder/(.*)$ /$1 [R=301,L]
Or you could place the following .htaccess in /subfolder:
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]
Note that the () around .* and the $1 redirects /subfolder/someFile.php to /someFile.php. If you skip it, everything in /subfolder redirects to /.

Thanks to #mariusnn in the comments, I was able to solve this issue.
❌ Not Working: .htaccess redirect FROM subfolder to domain name
RewriteEngine on
RewriteRule ^subfolder/(.*)$ /$1 [R=301,L]
✅ Working: .htaccess redirect entire subdomain "/subdomain/" and files "/subdomain/file_01.php" within are redirected to domain name "example.com"
RewriteEngine on
RewriteRule ^subfolder/.*$ / [R=301,L]
*Note that the () around .* and the $1 redirects /subfolder/someFile.php to /someFile.php. If you skip it, everything in /subfolder redirects to /.

This does the trick:
RedirectMatch 301 ^/subfolder$ http://yourdomain.tld/

Try:
RewriteEngine On
RewriteRule ^subfolder/index.php$ /index.php[NC,L,R]

Tried all answers here. This is what worked for me:
RewriteEngine on
RewriteBase /
RewriteRule ^subfolder/(.*)$ /$1 [R=302,NC,L]
Change R=302 to R=301 after you've validated the changes.

Related

Redirect to other domain except one subdirectory which needs to be redirected to a subdomain

I need to redirect a domain to other domain. However, one subfolder needs to be redirected to a subdomain. For example:
http://old.com/archives/ -> http://new.com/archives/
http://old.com/blog/archives/123 -> http://blog.new.com/archives/123
I added the following in .htaccess under the root folder of the old domain:
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/blog/(.*)$ http://blog.new.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
It works for all URLs except the subdomain blog.
For most URLs with the subdomain blog, it redirects to http://new.com/.
In this case, how can I edit it?
Try with :
RewriteEngine on
RewriteRule ^blog/(.*)$ http://blog.new.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
Because htaccess RewriteRule left url does not begin with /

Remove URI segment using htaccess

Here is my url:
http://localhost/BA/cookies-policy/register
I want that page to direct to :
http://localhost/BA/register
...and so on if the above link is accessed.
I am not familiar with htaccess.
Try this in .htaccess file in root directory:
RewriteEngine on
RewriteBase /
RewriteRule ^/BA/(.+)$ /$1 [L,QSA]
Basic .htaccess 301 redirect rule for one specific URL:
Redirect 301 /BA/cookies-policy/register http://localhost/BA/register
Rewrite rule to match your needs:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^BA/(.*)$ /$1 [R=301,L]
Try it like below, in your BA directory.
RewriteEngine On
RewriteRule ^cookies-policy/(.+?)$ /BA/$1 [R=301,L]

Redirect subdomain of one domain to a subdomain of another domain

I am trying to redirect the following (respectively):
http://sub.firstdomain.com/d/(all_files_and_folders)
http://sub.firstdomain.com/d2/(all_files_and_folders)
to
http://sub.seconddomain.com/d/(all_files_and_folders)
http://sub.seconddomain.com/d2/(all_files_and_folders)
There are other files and folders under the first subdomain that I do not want redirected. Previously this was working but now it seems like Go Daddy changed something and what I had is no longer working. Here it is:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.+) /dstats/count.php?statspage=$1 [L,NC,QSA]
RewriteRule ^(.+)\.deb$ /dstats/count.php?file=$1.deb [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(d2?)/(.*)$ http://sub.seconddomain.com/$1/$2 [L,R=301]
You can ignore the RewriteRule. It is working fine. I wanted to make sure I included the entire .htaccess file just in case. My issue is with RewriteCond it seems.
The following should work:
RewriteEngine On
Redirect 301 /d/ http://sub.seconddomain.com/d/
Redirect 301 /d2/ http://sub.seconddomain.com/d2/
The above should only redirect anything in the /d/ and /d2/ folder of the sub subdomain.
EDIT: Second try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1\.
RewriteRule ^(d2?)/(.*)$ http://sub2.mydomain.com/$1/$2 [L,R=301]

301 redirect subdomain to another domain with htaccess

I want to 301 redirect a subdomain to another domain with htaccess.
I want:
A: www.subdomain.domain1.se
B: subdomain.domain1.se
C: subdomain.domain1.se/anything/anything.anything#anything?anything
to redirect to:
A: www.domain2.se
B: www.domain2.se
C: www..domain2.se/anything/anything.anything#anything?anything
Also I need to know where to put the file (in subdomain directory or root directory). Best would be if I could put the htaccess file in the subdomain ddirectory if possible.
I have tried this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^c\.domain1\.se$ [NC]
RewriteRule ^(.*)$ http://www.domain2.se/$1 [QSA,R=301,L]
I think you messing RewriteBase / in the code
regular redirect 301 work like this
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain\.domain1\.se$ [NC]
RewriteRule ^ http://www.domain2.se%{REQUEST_URI} [R=301,L,NE]
Reference: Apache mod_rewrite Introduction
However note that URL part after hash is not sent to web server hence cannot be handled by Apache mod_rewrite.

.htaccess redirect folder to a url

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

Resources