I am working on a CakePHP project in shared hosting with multiple subdomains. Due to problems with Cake's htaccess I had to move the main site into a subfolder, and write a new htaccess to redirect users to this folder (while leaving the subdomains requests intact). At the minute my htaccess file looks something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteRule (.*) /domain/$1
</IfModule>
This works for requests with 'www' prepended to the url, but there are some issues with http: // domain.com requests. In IE & Chrome this address resolves itself to the 'www' url, but in Firefox & Safari, it shows the directory structure.
I need to figure out how to include the http: // domain.com requests in the rewrite conditions without affecting the other sub-domains.
Any advice would be greatly appreciated. Thanks. Adrian
Change your %{HTTP_HOST} rewrite condition as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain/ [NC]
RewriteRule (.*) /domain/$1 [L]
</IfModule>
The ? in (www\.)? says zero or one occurrence i.e. makes it optional.
[L] marks it as last i.e. rewriting should stop at this rule.
[NC] makes all matches case-insensitive.
Try this for the host match:
^(www\.)?domain\.com$
Related
I have...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
So, /in --is-really--> /login.php
This much works great. We all can learn how to do this from: .htaccess redirect with alias url
But, I want it to also work in reverse...
If someone should enter /login.php into the address bar, I want it to change to /in.
So also, /login.php --rewrites-to--> /in
From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
That also works great.
But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...
| .htaccess : (v3) —not working!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...but then /in and /login.php both cause an infinite redirect loop.
What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?
These Questions did not help:
Rewrite rule to hide folder, doesn't work right without trailing slash
This is not about a trailing slash
Allow multiple IPs to access Wordpress Site Admin via .htaccess
This is not about IP-based access
Htaccess URLs redirects are working for http not all https
This is not about https vs http
Rewrite-rules issues : .htaccess
This is not about cleaning up the GET array in the URL
apache htaccess rewrite with alias
This is not about rewriting the host/domain, thereby preserving the path
rewrite htaccess causes infinite loop?
This is not about www subdomain rewrites
.htaccess rewrite page with alias
This is not about rewriting "pretty" URLs nor about how to use slug settings in WordPress
Htaccess alias or rewrite confusion
This is not about simply having multiple rules with the same destination
htaccess rewrite to include #!
I'm not trying to rewrite #!
Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.
Suggested .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.
Thanks to the help from the user with the correct answer, I found that...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...doesn't go in .htaccess only once, but every time on the line before...
RewriteCond %{REQUEST_URI} ...
I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.
This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>
When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.
Here is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>
What am I doing wrong?
You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:
In VirtualHost
ServerName example.com
ServerAlias *.example.com
DocumentRoot /path/to/your/doc/root
In htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]
I've created an .htaccess file that contains redirects for one site that is part of a larger WordPress Multisite install.
The .htaccess content starts with this (necessary because the same .httacess file must be used for multiple sites:
RewriteCond %{HTTP_HOST} ^mydomain.com [nc]
And then contains a series of rewrites, like so:
RewriteRule ^about-my-site$ about [R=301,NC,L]
If I visit mydomain.com/about-my-site, I am correctly redirected to mydomain.com/about
However, if I visit mydomain.com/about-my-site/ (please note trailing slash), I get a "Page not found" error.
Change your RewriteRule to
RewriteRule ^about-my-site/?$ about [R=301,NC,L]
These rules should go before your WordPress rules, make sure that rewriting is on, and set a rewrite base of / -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} mydomain.com [NC]
RewriteRule ^about-my-site/? about [R=301,NC,L]
</IfModule>
Test here: http://htaccess.madewithlove.be/
input url
http://mydomain.com/about-my-site/
output url
http://mydomain.com/about
debugging info
1 RewriteEngine On
2 RewriteBase /
3 RewriteCond %{HTTP_HOST} mydomain.com [NC] This condition was met
4 RewriteRule ^about-my-site/? about [R=301,NC,L] This rule was met, the new url is http://mydomain.com/about
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301
Having issues trying to do a proper .htaccess rewrite with the following conditions
original URL:
http://example.com/foo/bar/ANYTHING (either http or https, ANYTHING could be anything the user enters)
rewritten URL:
https://example.com/foo/bar/index.php/this/that/ANYTHING
but don't rewrite anything with index.php already in it.
I've tried several options, but I keep getting the filesystem path included in the new URL.
My attempt:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /foo/bar/
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{HTTP_HOST} !index.php
RewriteRule ^(.*)$ https://example.com/foo/bar/index.php/this/that/$1 [R=301,L]
First off, test your rules without 301, because the browser caches 301 results and makes testing much harder. Add R=301 not until you're satisfied with the rules.
See also Tips for debugging .htaccess rewrite rules.
You don't need to test for example.com, unless multiple domains are hosted in the same directory.
Testing for index.php is against REQUEST_URI, not HTTP_HOST
RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^/?foo/bar/(.*)$ https://example.com/foo/bar/index.php/this/that/$1 [R,L]
I have a wildcard subdomain (ServerAlias *.mydomain.com) to catch all subdomains requests and redirecting them to the home page while keeping the "fake" URL in the browser and passing the subdomain (city name) as a URL parameter
.htaccess in the subdomain folder:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P,L]
</IfModule>
The problem: there is a menu with some links in every page (register.php, login.php, contact.php) and if you select any of them while being in one subdomain, the request (city.mydomain.com/login.php for example) is captured by the condition/rule
I guess I need to add a second set of condition(s)/rule but after some tests can't find the right one. I added this before the one already working:
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
RewriteRule ^(.*)$ http://%2.%3/%4 [P,L]
receiving the error:
Bad Gateway!
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /register.php.
Reason: DNS lookup failure for:
If you think this is a server error, please contact the webmaster.
Error 502
city.domaim.com
CentOS 5
Parallels Small Business Panel 10.2.0
Thanks in advance
You may be over complicating this by using mod_rewrite. I'd suggest simply to parse/validate the subdomain (city) and set a constant with straight PHP. For example in a config/init file. In the end, you'll have far greater control over the subdomain, especially in the case of validation.
KISS :)
The error was a naive one, I overlooked that back-references in the substitution string to RewriteCond pattern (%N), refer to the last one matched. Then, in my original rule, %2, %3 and %4 didn't make any sense
Here is the .htaccess working:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/%1.php [P]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P]
</IfModule>
Besides, the [P] flag implies [L]
The lesson: RTFM! :-)