DNS 301 redirect issue - dns

I implemented a DNS-level 301 redirect in my Namecheap console, which redirected my old sub-domain assets.websitename.com to websitename.com/assets.
However, when trying to access assets.websitename.com/css/main.css, the client will be redirected to websitename.com/css/main.css/assets. I intended to redirect the user to websitename.com/assets/css/main.css.
So my question is:
How can I properly configure the 301 redirect?

Apache
If using .htaccess or similar in Apache web server:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} assets.websitename.com$ [NC]
RewriteRule ^(.*)$ http://websitename.com/assets/$1 [L,R=301]
Namecheap
Since you're using the Namecheap control panel, try this:
Make sure you have a slash at the end of the IP Address / URL box. So it should look like this:
HOSTNAME IPADDRESS/URL REDIRECTTYPE
assets http://websitename.com/assets/ URL Redirect
notice the slash at the end of http://websitename.com/assets/

Related

.htaccess remove www with GCP hosted site

I have a website hosted on GCP bucket and I use cloudflare for HTTPS.
How can I use .htaccess file to remove www. from all website pages and redirect them to HTTPS?
Basically, I want:
http://www.example.com/ redirect to https://example.com/
https://www.example.com/ redirect to https://example.com/
and any other pages like
https://www.example.com/about.html or http://www.example.com/about.html redirect to https://example.com/about.html
I found the configuration for the .htaccess file that I need to add to the root directory of my website:
Catch all and redirect www to no-www (HTTPS):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]
I replaced example.com with my domain name, put the file into the root directory of my GCP bucket and nothing happened. If I try to access my website by http://example.com I get DNS_PROBE_FINISHED_NXDOMAIN error. I tried to save .htaccess with ANSI and with UTF-8 with the same result.
How do I:
Remove www. from my website using .htaccess
Redirect users to https:// without www.
What encoding should I use for .htaccess? Does it matter?
Is this even possible with my GCP bucket hosted static website and cloudflare provided https://?
I tried setting up redirects through my domain registrar's control panel, but they don't support redirects with dns nameservers other than their own default ones.
The simplest solution would be to use CloudFlare to take care of HTTPS and the www site redirection.
To force HTTPS, go to CloudFlare settings: SSL/TLS => Edge Certificates => Always Use HTTPS => On.
Always Use HTTPS:
Redirect all requests with scheme "http" to "https". This applies to
all http requests to the zone.
To redirect some locations to some other locations, you can create a page rule. See example https://support.cloudflare.com/hc/en-us/articles/200172286-Configuring-URL-forwarding-or-redirects-with-Cloudflare-Page-Rules
There could be further issues at play here (It has been a while since I've messed with Apache), but the unescaped periods in your regex for the condition will definitely cause issues. Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

.htaccess redirect for https://.co.nz website to .com version(including all subpages)

I am palling to migrate my website from the .co.nz to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly without any 404 pages from .co.nz version
All https://
I want "https://www.example.co.nz/" to go to "https://www.example.com"
Which will also redirect
https://www.example.co.nz/contact to go to https://www.example.com/contact
https://www.example.co.nz/men to go to https://www.example.com/men
Many Thanks
This should do the trick:
Redirect 301 / https://www.new-example.com/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ https://new-example.com/$1 [L,R=301]
You put this in the .htaccess file of the .co.nz domain. This will redirect all your content and subfolders to their appropriate subfolders on the new domain (so example.me/subfolder to example.com/subfolder) AND also force redirect to https part.

Append www into URL using htaccess

I have a WordPress site which works fine if I access it through the full name of the domain, example www.example.com. However if I try to access it without typing the www e.g example.com, I get the following error:
Origin http://example.com is not allowed by Access-Control-Allow-Origin.
The only solution I can think of is to append / rewrite any request to example.com to www.example.com. How can I achieve this with .htaccess?
Add this above any wordpress rules in your htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [L,R=301]
For those who encountered this with WordPress. I got it fixed by installing this plugin https://github.com/jacopotarantino/WordPress-Cross-Domain-Plugin and add the site into "Allowed Domains".

Redirect domains and subdomains with .htaccess

Right now *.foo.com and *.bar.com are aliased to the same vhost with LAMP. Right now it, of course, shows the same content with every URL. But I don't actually want this to be what happens, and I want the other domains to actually redirect to foo.com. I'm assuming this is possible with the .htaccess file.
EDIT: I'm hosting this on my own server, so I'd also like the IP address to redirect to foo.com
Use a rewrite condition in your .htaccess to selected everything not for foo.com and redirect it then:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foo\.com$
RewriteRule ^(.*)$ http://foo.com/$1 [R=301,L]

302 .htaccess redirect on joomla 1.5

What's the best way to temporary redirect my entire site to a subdomain in joomla?
For example redirect mydomain.com (including all my website pages) to offline.mydomain.com.
What string should I add in .htaccess?
I want to be sure that all visitors are not able to visit mydomain.com and all subfolder and files when redirect is on.
You could place a .htacces with following content:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule (.*) http://offline.mydomain.com/? [R=302,L]
so everybody except the User with IP 127.0.0.1 will get redirected by it.

Resources