Use .htaccess to force domain for all file paths - .htaccess

I have a website setup on a test server and the web address is along this format:
http://01.23.45.66/~websitename/
The site loads, but all images are calling http://01.23.45.66/images and not http://01.23.45.66/~websitename/images. Same for javascript files and CSS.
How can I set the .htaccess file to force this second URL segment for file paths?

You can use this rule as your first rule in Apache config or site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:images|js|css)/ [NC]
RewriteRule ^ /~websitename/%{REQUEST_URI} [L,NE,R=301]

Related

htaccess redirect between subdomains depending on the file extension

Situtation
I have 2 subdomains www.example.com and static.example.com and they both read the files on the same folder. So:
If I go to https://static.example.com/privacy I see the same that I see on https://www.example.com/privacy
If I go to https://www.example.com/src/file.js I see the same that I see on https://static.example.com/src/file.js
Also, both subdomains read the same .htaccess file.
What I want to do
I want is to redirect any request of an image, video, js or css file from www to static and any other file type from static to www.
Expected Behavior
That way, if I go to https://static.example.com/privacy I should be redirected to https://www.example.com/privacy
And, if I go to https://www.example.com/src/file.js I see the same the is on https://static.example.com/src/file.js
You can do something like the following:
# Identify static resources - set an environment variable
SetEnvIf Request_URI "(?i)\.(jpg|png|gif|mpg|mp4|wmv|js|css)$" STATIC_RESOURCE
# Redirect static resources to static subdomain
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{ENV:STATIC_RESOURCE} =1
RewriteRule \.([\w]{2,4})$ https://static.example.com%{REQUEST_URI} [R=301,L]
# Redirect "other" requests to www
RewriteCond %{HTTP_HOST} ^static\.
RewriteCond %{ENV:STATIC_RESOURCE} ^$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Although, I assume your site is already linking to the appropriate subdomain?
You could just force a 404 instead?

Rewrite Directory Based on Subdomain

I have a folder export that is accessible to all my subdomains:
/export/sub1/...
/export/sub2/...
/export/sub3/...
Right now, regardless of what subdomain you're on, you can see all of the content by changing the directory in the url.
It's not a security issue, but more of a canonicalization concern, but I'd like to use an .htaccess file to rewrite the folders so people see a modified path that matches up with their subdomain:
sub1.domain.tld/export/... is served from /export/sub1/...
sub2.domain.tld/export/... is served from /export/sub2/...
sub3.domain.tld/export/... is served from /export/sub3/...
How can I do this?
You can use this generic rule in site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+) [NC]
RewriteRule ^/?export/(.*)$ /export/%1/$1 [L,NC]

RewriteCond if url is not "cdn.domain.com/images/"

we have created a CDN subdomain to host images, using this URL:
https://cdn.example.com/images/
What we want is to redirect, if someone goes to any other path of this subdomain. For example:
https://cdn.example.com/
https://cdn.example.com/blabla/
Redirect to other domain, just not redirect if the folder is images.
create htaccess file in your document root, and deny direct access to all files
Order deny,allow
Deny from all
and create htaccess file in images:
Allow from all
Try the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn.example.com [NC]
RewriteRule !^images/ https://example.com%{REQUEST_URI} [R,L]
If you access cdn.example.com/<something> and the URL-path does not start /images/ then redirect to https://example.com/<something> (the main domain).
Change R to R=301 (after you've made sure it is working OK) if you want a permanent redirect.
UPDATE: To exclude additional folders (eg. images2 and images3) then you can change the RewriteRule directive to:
RewriteRule !^(images|images2|images3)/ https://example.com%{REQUEST_URI} [R,L]
If the folders are literally called images, images2 and images3 then this could be simplified to match (or rather not match) any URL of the form /imagesN - where N is an optional digit:
RewriteRule !^images\d?/ https://example.com%{REQUEST_URI} [R,L]

htaccess configuration directories for subdomains and root domain

My folders in my www directory as set up as follows:
www/forums
www/helpdesk
www/www
In the base www folder (not www/www), I have the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I also have the following set up for configuring the index.php on the main site (www.example.com) - this is also in the htaccess file in the www directory:
# Rewrite rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ www/$1 [L,QSA]
The problem I am facing is that I have stuff from forums.example.com that I want to embed in www.example.com (Vanilla Forums + WordPress plugin) - if I configure this using the admin panels, the iFrame gets blocked because they are different domains.
I found out that when I go to www.example.com/forums, I get the same front page as forums.examples.com - but all the clean URLs break.
When I look at the .htaccess file in the www/forums folder, I see the following configuration
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php\?p=$1 [QSA,L]
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com
...the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
Aside: That snippet is just a canonical non-www to www redirect. It doesn't "redirect based on subdirectory" nor does it "direct [any] subdomains". (?)
the iFrame gets blocked because they are different domains.
It sounds as if you need to set an Access-Control-Allow-Origin HTTP response header on forums.example.com to allow the content to be "embedded" in example.com? Something like the following (using mod_headers) in the www/forums/.htaccess file:
Header set Access-Control-Allow-Origin "http://example.com"
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com.
How do "all the clean URLs break"? However, given the HTTP header mentioned above, you shouldn't have to do anything more and still access forums.example.com (not the subdirectory).
Incidentally, the fact you can access the subdirectory (that the subdomain points to) is just how your hosting is configured. Normally you should block access to the subdirectory in order to prevent duplicate content issues (and any other issues, such as the linking problem you mention).
Also note, that due to the way mod_rewrite directives are inherited (or not in this case). The mod_rewrite directives in the www/forums/.htaccess file completely override the mod_rewrite directives in the parent folder.

Having one file non-https on https site and how to write the code in the htaccess file

Our software uses a file on our website to check if it is valid and is hardcoded into the software, I want our site to go to https but want the directory for this file left as http.
You can add .htaccess to your website root directory with following configuration:
# This will enable the Rewrite capabilities
RewriteEngine On
# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on
# This checks file name is not file.txt
RewriteCond %{REQUEST_FILENAME} !file.txt
# This redirects to all requests to https
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Please check this reference for more info.

Resources