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.
Related
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.
I'm running a site in 2 environments: production and development.
Each server has a vhosts.conf file, which sets environment variables. One of these variables is the correct subdomain to use.
vhosts.conf (development server)
RewriteRule .* - [E=DEV_OR_WWW:dev]
vhosts.conf (production server)
RewriteRule .* - [E=DEV_OR_WWW:www]
I also have a .htaccess file, which is identical on both servers. I am trying to rewrite old domain urls into new ones, while preserving the subdomain, like this:
.htaccess
RewriteCond "%{HTTP_HOST}" "%{ENV:DEV_OR_WWW}\.olddomain\.com" [NC]
RewriteRule "^/(.*)" "http://%{ENV:DEV_OR_WWW}.newdomain.com/$1" [L,R=301]
But the RewriteCond above does not work. %{ENV:DEV_OR_WWW} is not matching www or dev, and the rule is not applied. How can I use a variable to match the HTTP_HOST against?
If you need more details, or if my question is unclear, just ask.
You can use the following :
RewriteCond %{ENV:DEV_OR_WWW} ^(.+)$
RewriteCond %1.olddomain.com ^(www|dev)\.olddomain\.com$ [NC]
RewriteRule /(.*) http://%1.newdomain/$1 [L,R=301]
In my web root (var/www) I have the following in my .htaccess file:
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|vges|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I then have a subdirectory (/var/www/test) with a codeigniter project. It also has a .htaccess file with the following:
RewriteEngine On
RewriteCond $1 !^(index\.php|quiz|vges|buddy|css|fonts|img|images|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
When I visit localhost I can view the test directory and when I go to the test directory I'm displayed with the default controller view. But when I visit localhost/test/controller I get a 404 not found error:
The requested URL /index.php/events was not found on this server.
Edit: I found the solution by changing the last line /var/www/vges.htaccess to:
RewriteRule ^(.*)$ test/index.php/$1 [L]
Although I suspect if I was to upload this test project to a server, where it is not a subdirectory then I may get an error. What's the best solution to this? What should
I change in my .htaccess file so
I can have subdirectories on my local machine
When I upload them as individual projects (such as the test project) to a server I don't have to modify the .htaccess file.
Thanks.
First, .htaccess files apply to the directory they're in, and every child directory. Your CodeIgniter-specific rules should be in the var/www/test/ CI directory -- usually wherever CI's index.php file is.
Second, your root .htaccess is kind of weird. RewriteCond conditions accumulate until there a RewriteRule rule fires, then they are reset. Your two %{REQUEST_URI} conditions conflict with each other, since the URI can't start with both. I'm not really sure what this .htaccess rule is doing, but if your URLs starting at the root have no bearing on your CI application, I don't think it's necessary to have it there in the first place. I can't say for sure without knowing your directory structure and how you want your website to function.
kjetilh is right - unless your environments share exactly the same settings, it's unlikely an .htaccess will be universally functional. Your best bet is to start them as simple as possible, and modify as necessary. A good starting .htaccess for CI 2.1.x is something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You can add your assets directories or any paths that you don't want redirected in the third RewriteCond (remember to properly escape regular expression characters such as periods). A RewriteBase rule definitely comes in handy if CI is in a subdirectory. You also don't need the system and application folder references with 2.1.x, since those folders have their own .htaccess files blocking access to them.
I have a question.
I am trying to setup a TEST, CERT and PROD webspace on my hosting with 1 domain.
The problem is that I am already using subdomains in my webpage so DNS wildcard record configuration is not possible.
The url looks as following on PROD:
company1.domain.com or company2.domain.com
Both pointing to the same directory on the hosting.
What I want to achieve is:
company1.domain.com -> points to directory1
company1.test.domain.com -> points to directory2
company1.cert.domain.com -> points to directory3
"company1" must be a wildcard.
Additionally I would not like to see the directory being displayed in the URL.
Anyone has an idea on how to accomplish this?
Should I remove existing DNS wildcard records?
Ideally, you should be making each of these subdomains into a separate vhost and point each vhost to its own directory. Some hosting companies even allow you to assign subdomains to separate directories.
But if for some reason, they all need to point to the same document root, try adding these rules to the htaccess file in the document root:
RewriteEngine On
# for company1
RewriteCond %{HTTP_HOST} company1.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/directory1
RewriteRule ^(.*)$ /directory1/$1 [L]
# for company1.test
RewriteCond %{HTTP_HOST} company1.test.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/directory2
RewriteRule ^(.*)$ /directory2/$1 [L]
# for company1.cert
RewriteCond %{HTTP_HOST} company1.cert.domain.com [NC]
RewriteCond %{REQUEST_URI} !^/directory3
RewriteRule ^(.*)$ /directory3/$1 [L]
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?