How do you use htaccess to redirect requests to a folder to a new subdomain? - html

I am moving an application from a folder off the root, to a subdomain. So I need requests to website.com/folder to redirect to folder.website.com. Below is what I tried, but it adds the original folder to the URL. For example, it does folder.website.com/folder/login, but what I need is folder.website.com/login.
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com
RewriteRule ^folder/(.*)$ https://folder.website.com/$1 [L,QSA,R=301]

If you need this rule to only be applied to the folder route, then give the following a try replacing the following rule:
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com
RewriteRule ^folder/(.*)$ https://folder.website.com/$1 [L,QSA,R=301]
WITH
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ http://www.website.com$1 [L,R=301]
RedirectMatch 301 ^/folder/(.*)$ https://folder.website.com/$1
You may already have the first 2 lines added.

Related

Redirect from one website to another and keep directory

The blog section of a website moved from a subdomain to a directory of the main domain. I need to redirect all users visiting blog.site.com to site.com/blog/ and the directory someone enters should be kept when forwarding, e.g. blog.site.com/post/welcome should point to site.com/blog/post/welcome
I treid adding this to my .htaccess file which I placed in the subdomain's directory via FTP:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.herderzeitung.de$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.herderzeitung.de$
RewriteRule (.*)$ https://herderzeitung.de/blog/$1 [R=301,L]
However, this doesn't seem to be working. I was also wondering, what the most SEO friendly way is to achieve this (the site permanently moved to another domain).
Thanks in advance!
Try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.herderzeitung\.de$ [OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.herderzeitung\.de$
RewriteRule ^/?(.*)$ https://herderzeitung.de/blog/$1 [R=302,L]
I've escaped the dots, and anchored&normalized the pattern. Also switched it to temporary redirects (302); once you know it works, then change it to 301 again for SEO reasons.
And yes, as far as i know 301s are the best method for retaining your search rankings and the like.
This seems to work:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://herderzeitung.de/blog/$1 [L,R=301]
Thank you everyone!

Redirect From Pretty URL to .html URL

I have recently switched from using Wordpress to a static html only site. When I remade each page (there are only 6 pages total) I kept the same URL name.
Example: https://reactiongaming.us/alteriwnet/ is now https://reactiongaming.us/alteriwnet.html
My issues is that old links and Google search results contain the reactiongaming.us/alteriwnet/ version which results in a 404 error. I have tried various .htaccess methods for "pretty URLs but none worked. Usually, these questions were asked on how to redirect from a .html URL to a pretty URL, but I'm trying to do the opposite.
There are only 6 URLs I need to redirect in total from reactiongaming.us/example/ to reactiongaming.us/example.html. Is there an .htaccess or other method to achieve this?
I have tried:
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
You can use Redirect directive to redirect your old URLs to the new URL format
Redirect 301 /alteriwnet/ /alteriwnet.html
This will 301 redirect /alteriwnet to /alteriwnet.html .
To redirect multiple URLs with just a one liner code you can use RedirectMatch which uses a regex based pattern
RedirectMatch 301 ^/(alteriwnet|path2|path3)/?$ /$1.html
References :
Redirect directive
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
RedirectMatch directive
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectMatch

htaccess why is site redirecting to //

In my htaccess I am redirecting all non https to https.
It works, but its also adding in an extra '/', so the url is
'https: //www.[MY SITE URL].co.uk//'
Why is this? To be honest, I don't really know what all this in my htaccess is doing, its copied from googling answers to 'redirect all requests to https'
My htaccess:
Options -MultiViews
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
#RewriteCond %{HTTP_HOST} ^(www\.)?jobooz\.com [NC]
#RewriteRule ^(.*)$ https://www.jobooz.co.uk/$1 [R=301,L,NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1
RewriteRule ^/?$ /php/index.php [NC,L] # Home page
I've also noticed any deeper urls like '/search-jobs/jobs-near-me' that I add to the url then get duplicated too, to '/search-jobs/jobs-near-me/search-jobs/jobs-near-me' when redirected to https.
All urls work fine if I go directly to the https version.
Any help appreciated, thanks.
The following rewrite rule has a problem:
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
The gist of why you are seeing repeated fragments in the rewritten URL is that (.*) matches everything, and that already includes the host and URI. Instead, you can try redirecting any incoming request on port 80 to HTTPS.
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

.htaccess conditional redirect https to http

I'm trying to make all https requested to my site route to http EXCEPT for anything at /shop
Using the following code in my .htaccess but it's not working:
#redirect all https traffic to http, unless it is pointed at /shop
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/shop/?.*$
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
Hosted on rackspace cloud sites.
Other code in .htaccess
RewriteEngine on
RewriteBase /
# Shop rewrite rule + remove trailing slash
RewriteRule ^shop\/(.*)/$ shop/$1 [R=301,L]
RewriteRule ^shop\/([A-Za-z0-9-/]+)$ shop.php?shop=$1 [NC,L,QSA]
DirectoryIndex index.php index.html index.htm

modrewrite to remove the file extension but still load the page

I have a subdomain with .php pages in it. I just want to remove the .php I have written some code gathered from other posts on stack, so far I have this
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (\.php(.*)\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
this rewrites
subdomain.example.com/weddings.php
to
subdomain.example.com/weddings
However it also creates a 404 page not found error. Am I doing something wrong?
Solved it. anyone struggling with the same thing here is my solution
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
found it on
http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
a fantastic website.