.htaccess rewriterule to path outside wwwroot - .htaccess

I want to change the document root of my web folder. I don't have access to the httpd.conf or the virtual host definition so I can't change it there.
I saw this question: Using htaccess to change document root and know you can use to rewrite the incoming URL to a new path.
The document root is on this path: /httpdocs/*
And I want it to rewrite everything to /subdomains/new/httpdocs/*
If I look at the 'normal' rewrite rule it rewrites to a relative path:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteRule (.*) /folder/$1 [L]
How is it possible to rewrite outside the wwwroot?

Related

.htaccess - hiding subfolder1 in URL but showing subfolder2 (other website)

I have a wordpress website physically located in the "wordpress" subfolder of the root folder of the website. I manage to hide the subfolder "wordpress" in the URL with the following code:
.htaccess on root folder
RewriteEngine On
RewriteBase /
RewriteRule ^$ wordpress/ [L]
RewriteRule ^(.*)$ wordpress/$1 [L]
.htaccess in wordpress subfolder
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
However, I have another website in another subfolder let's call it "other-wp" and this needs to remain as it is, with the URL pointing to:
https://mywebsite.com/other-wp/
Since I managed to hide the "wordpress" folder in the URL, I am unable to acces my "other-wp" it says the page doesn't exist.
I'm not skilled with coding for .htaccess so i don't know what i need to do to fix it.
Could you help?
You need to implement an exception for that second resource:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/other-wp
RewriteRule ^/?(.*)$ /wordpress/$1 [L]
I also made some other modifications to that top level configuration file, just smaller optimizations. In general you should check if you can place such global rules in the actual http server's host configuration. Using distributed configuration files (".htaccess") is just a fallback if you have no access to the real configuration. They work, but come with disadvantages.

htaccess rewrite folder with different paths

I have an web application setup on a local and staging server. The local application has a different basepath to the staging. e.g.
Local
http://phils-imac.local/git/clients/myproject/html/
Staging
http://myserver.com/myproject/html/
I would like use htaccess to make the urls accessible without the 'html' part. e.g.
http://phils-imac.local/git/clients/myproject/
http://myserver.com/myproject/
I have been using this rewrite rule on my staging server:
RewriteEngine On
RewriteCond %{SERVER_NAME} =myserver.com
RewriteCond %{REQUEST_URI} !^/myproject/html/.*$
RewriteRule ^(.*)$ /myproject/html/$1 [L]
It works ok but I feel I need to customise it for each project. Ideally I'd like the 'myproject' part to be a variable and have the rule more general so it would also work on my local path.
You can't define variables in your .htaccess but if you keep your project name same you can accommodate for a variable base path in your rewrite rule as
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/html/ [NC]
RewriteRule ^(.*?)/?myproject/(.*)$ $1/myproject/html/$2 [NC,L]
This requires your .htaccess in your web root directory. If your .htaccess resides inside your myproject folder try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/html/ [NC]
RewriteRule ^(.*)$ html/$1 [L]
In both the cases, you need to drop your %{SERVER_NAME} check as it would never match between your local and staging locations.

htaccess rewrite subfolder to root

In my root folder I have an .htaccess file that redirects URLs to a subfolder as follows. This folder contains a coming soon page:
RewriteCond %{HTTP_HOST} ^(www.)?myotherurl.com$
RewriteRule ^(/)?$ test [L]
When this rule is applied I get the following url: http://myotherurl.com/test/
Now I would like to rewrite (not redirect) the shown URL to http://myotherurl.com by creating a nested .htaccess. What should this file contain?
Try this one:
RewriteCond %{HTTP_HOST} ^(www\.)?myotherurl.com$
RewriteRule ^/?(.*)$ test/$1 [L]
This puts everything to the target directory. In your condition you missed to mask the dot. This does not matter in this case but it would also match the domain wwwamyotheeurl.com
If you have just one static page you could also use this rule:
RewriteRule ^ test/index.html [L]

How can I rewrite all requests for one domain to a subfolder (htaccess)? Existing code not quite working

I have the following rewrite rules in an .htaccess file. The idea is any requests to www.site.com should be routed to the 'folder' subfolder, but any other domains should be unaffected.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.site\.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ /folder/$1 [L]
This works for the index file in /folder/, but I have images and css in /folder/assets/ and these are not loading.
Any idea how to pass on the rewrite to the entire contents of /folder/, including files in /folder/assets/ ?

.htaccess - redirect links on page by removing folder from link

I followed this tutorial to manage subdomains for a MODx Revo 2.1.3 installation. The idea is that the htaccess file in the web root folder redirects calls for subdomain.mydomain.com to a folder in the web root directory by the same name as the subdomain, ie /home/mydomain/www/subdomain. Subdomain contains the MODx files to run the page, as well as another htaccess to point all further requests back to the root folder.
The better part of it works; I can view the homepage of the site (which means MODx is doing its part), but none of the links to the css, js, images, etc work, and it seems like the Wayfinder and getResources packages are failing to output. The links that are being used for the images+css+js are /subdomain/assets...etc; I need the links to point directly to the /assets folder. It's like the root .htaccess works to redirect the request to the subdomain folder, but the .htaccess in the folder doesn't point anything back up to the root for the remaining requests.
Here's my root folder htaccess, the 2nd part taken from the tutorial:
RewriteEngine On
RewriteBase /
# The Multiple subdomains part
#REDIRECT SUBDOMAIN TO SUBDIRECTORY OF SAME NAME
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.mydomain\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
and the subdomain folder htaccess is simply:
RewriteEngine On
RewriteBase /
I know the site works; I can access it using a subdomain that hasn't been processed like the tutorial yet. So it's all there, I just need to sort out the link requests. Can anyone help?
Thanks to some assistance, I managed to find the solution to this issue.
# The Multiple subdomains part
#REDIRECT SUBDOMAIN TO SUBDIRECTORY OF SAME NAME
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.spitfireresources\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
#RewriteCond %{REQUEST_FILENAME} !^/js
#RewriteCond %{REQUEST_FILENAME} !^/css
#RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
The key line is RewriteCond %{REQUEST_URI} !^/. Once that was in place everything becomes accessible. Thanks to everyone who commented.

Resources