I want to open subdomain as folder, for ex: http://sms-rassilka-cityname.sitename.ru open like http://sitename.ru/cities/barnaul/ (real folder with files)
I used this rules for all cities:
RewriteCond %{HTTP_HOST} ^(www\.)?sms-rassilka-cityname\.sitename\.ru$ [NC]
RewriteCond %{REQUEST_URI} !^/cities/cityname/ [NC]
RewriteRule ^(.*)$ /cities/cityname/$1 [L,QSA]
But when I change this for universal rule
RewriteCond %{HTTP_HOST} sms-rassilka-([a-z]+)\.sitename\.ru$ [NC]
RewriteCond %{REQUEST_URI} !^/cities/%1/ [NC]
RewriteRule ^(.*)$ /cities/%1/$1 [L,QSA]
server fall down for error 500: Internal Server Error.
2d line don't gete paremetr, but in 3d it get.
What's wrong in my rules?
Try your rules this way.
RewriteCond %{HTTP_HOST} ^sms-rassilka-([a-z]+)\.example\.ru [NC]
RewriteRule ^((?!cities/.*).*)$ /cities/%1/$1 [L,QSA]
Related
I'm trying to use .htaccess to redirect a visitor with a specified IP address to always be redirected to a specified page when visiting any page on my website. I've tried the code below but it is causing an error. I'm using a wordpress site.
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule $ /specified-page/ [R=302,L]
It is causing "ERR_TOO_MANY_REDIRECTS" message.
As a side-note, my domain is pointing to a sub-folder in my webspace. As are some other websites. This is all directed from another .htaccess file in the root of my webspace with code like this.
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]
and so on...
(The .htaccess I'm trying to edit to create the IP redirect is in 'mywebsitefolder1' folder.)
Try it like that:
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule ^ /specified-page/? [R=302,L]
This will also remove an optional query string that could cause the redirect loop.
You have indeed a looping condition. Let's try it like this:
RewriteCond %{REMOTE_HOST} ^1\.2\.3\.4
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteRule ^ /mywebsitefolder1/specified-page/ [R=302,L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]
I have these rules in my htaccess. one is for the main domain of the server and the others are other domains that are pointed at the server.
the purpose is to redirect subdomains to a url that is written a certain way. the main domain excludes certain subdomains from the redirect.
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.maindomain\.com [NC]
RewriteRule ^(.*)$ https://maindomain.com/index/parser.php?%2&domain_name=maindomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.anotherdomain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/index/parser.php?%2&domain_name=anotherdomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.whatever\.com [NC]
RewriteRule ^(.*)$ https://whatever.com/index/parser.php?%2&domain_name=whatever.com [QSA,L]
I don't want to keep having to add a new 3 lines for each new domain pointed at the server. How do I create one rule set to incorporate any domain that is pointed at the server? I would still need certain subdomains to be excluded from the main domain, but it wouldn't matter if the same subdomains are excluded from all the domains.
here are a couple things I tried. neither works.
#attempt 1
RewriteCond %{HTTP_HOST} ^(?:www\.|)([a-z0-9-]+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
#attempt 2
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
Not sure if I solved it, but this seems to be working. not sure if i can make it even better to handle http and https at the same time, and to handle many exceptions for main domain but just the one exception for the rest, but this is satisfactory enough for my needs. most domains will be ssl, so only need to edit htaccess files to add non ssl domains.
RewriteEngine On
#remove www from any domain pointed at box whether it's ssl or not
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
#main domain with subdomain exceptions
RewriteCond %{HTTP_HOST} ^(.+)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^something\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^somethingelse\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/index/parser.php?%1&domain_name=maindomain.com [L,R]
#non ssl domains here. three lines each.
RewriteCond %{HTTP_HOST} ^(.+)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.com$ [NC]
RewriteRule ^ http://anotherdomain.com/index/parser.php?%1&domain_name=anotherdomain.com [L,R]
RewriteCond %{HTTP_HOST} ^(.+)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.whatever\.com$ [NC]
RewriteRule ^ http://whatever.com/index/parser.php?%1&domain_name=whatever.com [L,R]
#all ssl domains use this one rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteCond %{HTTP_HOST} !^www\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
I have these htaccess-rules:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.de\/subpage$ [NC]
RewriteRule ^(.*)$ http://www.domain.de/subpage.html/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.de$ [NC]
RewriteRule ^(.*)$ http://www.domain.de/subpage.htm/$1 [R=301,L]
I don't get it. Usually htaccess-redirects are no problem, but this here drives me crazy.
When I open mydomain.de/subpage it tries to open the folder (or throws an 404 for not existing resource). Instead I want my htaccess-rule to trigger. What am I missing?
Okay, got it. Starkeen gave me a direction. Had to use variatons of condition-types: this is my working result:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.de(/)?$ [NC]
RewriteCond %{REQUEST_URI} subpage
RewriteRule ^(.*)$ http://www.mydomain.de/new_page_html [R=301,L]
I have set up virtual subdomain. and i want to make htaccess to achive the following flow
For
www.domain.com
it should call the index file but for the subdomain
abc.domain.com
The rewrite rule should be like
RewriteRule www.domain.com/index.php?var=abc
i mean it should (in HTACCESS) pass the subdomain to the index file as an argument
and for the Other file requests like
abc.domain.com/file.php
The subdomain should be rewritten like www.domain.com/file.php?var=abc
I mean the Rewrite rule like
RewriteRule www.domain.com/file.php?var=abc
This would be how you could do them on an individual basis.
RewriteCond %{HTTP_HOST} ^abc\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=abc [L]
RewriteCond %{HTTP_HOST} ^def\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=def [L]
The solution is something like this
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
I have this code in my .htaccess to handle subdomains (http://foo.mydomain.com)
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]
The problem is what do i have to do if for example someone puts a link like this:
http://foo.mydomain.com/bar
i want to redirect this to another page different from the filter.php (say otherpage.php) from the previous code, i tried the next code but it isn't working
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com/([^/]+) [NC]
RewriteRule ^(.*)$ otherpage.php?type=country&country=%1 [L]
neither the next one:
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^(.*)$ otherpage.php?pname=$1 [L]
The link is always resolved with the first rule i wrote.
Add a condition to your first group that checks for an empty REQUEST_FILENAME. This will prevent it from matching when there's something other than a bare domain name.
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_FILENAME} ^/?$
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]