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! :-)
Related
I've checked many posts related to this topic, but I they didn't work as I expected, so... this is my question:
I have several pages with this pattern in my website:
http://example.com/newsletter/[variable_substring]
And I need to force all of those urls that contain "/newsletter/" as a part of the url to use "https://" protocol instead "http://" (if they are accessed by http://, of course).
I need to do this in the .htaccess. Anybody knows what exactly I had to type in?
Regards,
Try to add this to .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^/newsletter/(.*)$ https://%1/newsletter/$1 [R=301,QSA,L]
Also in httpd.conf add AllowOverride All for this directory
Try the following code
RewriteEngine on
#if the https is off
RewriteCond %{HTTPS} ^off$
#and the request is /newsletter/...
RewriteCond %{REQUEST_URI} ^/newsletter/
#then redirect it to https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
I have a domain (let's say www.example.com) and would like this to point to /Example_folder/ of my server (within /var/www/).
So, if I try to goto www.example.com/images/test.html or example.com/images/test.html, it should be actually pointing at /Example_folder/images/test.html.
I tried to get this working using following code, but I can't figure out.
Trial#1:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/ [L]
If I use above code, I get ERR_TOO_MANY_REDIRECTS.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/index.html [L]
If I use above code (where index.html is specified), it would redirect but I can't get my domain to point at its subdirectories. (www.example.com/images/test.html would also point at www.example.com/index.html)
I got it working using the code from link below:
htaccess Silent Redirect to Subdirectory: Subdirectory showing when no trailing '/'
Last thing that remains is that when I point to www.example.com/Example_Folder, I want the address bar to show www.example.com, but I have not figure that out yet.
You can use this negative lookahead based rule to avoid rewrite loop:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
Rewriterule ^((?!Example_Folder/).+)$ /Example_Folder/$1 [L,NC]
Which means rewrite only if URI doesn't already start with Example_Folder/.
I want to rewrite one specific url.
http://example1.com should be http://example2.de .
But http://example1.com/subdir or http://sub.example1.com should remain the same.
I found the following, which successfully rewrites example1.com, but also every url which starts with example1.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Background: I want to redirect the main page of an WP-Multisite but want to make sure that I can work with the backend of wordpress and run other multisites which are subdomains.
For matching only http://example.com domain (without possibility to add anything before or after the example.com) use the following code:
RewriteCond %{HTTP_HOST} ^(example.com(\/{0,1})){1}$
RewriteRule http://example2.de(\/{0,1}) [R=301,L]
That (\/{0,1}) part is for matching both example.com and example.com/ (but nothing esle) - if you do not wish to match example.com/ remove that part from both rows.
You're pretty close but you don't need to capture URI in $1:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^$ http://example2.de/ [L,R=301]
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$
I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]