Subdomain to folder universal rule - .htaccess

I want link any of subdomain to folder and save path after url.
For example, sms-rassilka-reklama-barnaul.mirsms.ru/somefile.txt need to be open in folder /cities/barnaul/somefile.txt
But my code with alphabetic part not to work.
RewriteCond %{HTTP_HOST} ^(www\.)?sms-rassilka-reklama-([a-z]+)\.mirsms\.ru$ [NC]
RewriteCond %{REQUEST_URI} !^/cities/%1/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ /cities/%1/$1 [L,QSA]

Try with:
RewriteCond %{HTTP_HOST} ^(?:www\.)?sms-rassilka-reklama-([a-z]+)\.mirsms\.ru$ [NC]
RewriteCond %{REQUEST_URI} !^/cities/%1/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ /cities/%1/$1 [L,QSA]
Because your first capture was www not your word

Related

RewriteCond blank domain to the domain with the correct path

How do I set redirects with following conditions:
Url http://domainname.be becomes http://www.domainname.be/nl/
Url http://www.domainname.be becomes http://www.domainname.be/nl/
Url http://domainname.be/nl/custompage/ becomes http://www.domainname.be/nl/custompage/
?
Rewrite conditions
RewriteCond %{HTTP_HOST} ^domainname.be [NC]
RedirectRule ^(.*)$ http://www.domainname.be$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domainname.be [NC]
RewriteCond %{REQUEST_URI} ^/$
RedirectRule ^(.*)$ http://www.domainname.be/nl$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domainname.be [NC]
RewriteCond %{REQUEST_URI} ^/$
RedirectRule ^(.*)$ http://www.domainname.be/nl$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/nl/custompage /Custompage.aspx?lang=nl [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/nl/custompage/ /Custompage.aspx?lang=nl [L,NC]
But is doesn't work. Also, do you need to clear the browsercache in order to have the latest conditions?
Remove all of your existing rules and use this:
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.be$ [NC]
RedirectRule ^((?!nl/).*)$ http://www.domainname.be/nl/$1 [R=301,L]
And yes test it out in a different browser to avoid 301 caching issues.

301 redirection through .htaccess file not working

I'm using CodeIgniter.
in httacess file, I wrote this code to remove index.php from url:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Now I want to redirect site root to subdomain, with this code:
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
domain.com redirected to sub.domain.com successfully. but domain.com/dir to sub.domain.com/dir not work and still opening domain.com/dir.
When I removed first code, redirection worked perfect, but I need to remove index.php too.
Adding $to the end of rewritecond %{http_host} ^domain.com [nc] should fix it for you.
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Would become
rewritecond %{http_host} ^domain.com$ [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Have your rules like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

How can I write my redirect so it does not show the directory?

If I'm visiting my website for the first time then it always displays the directory that I'm pointing my site to. For example, if I visit example.com, it redirects to example.com/site.2014/. If I remove the directory from the URL, and revisit, it doesn't show up anymore. Can anyone explain why this happens? I have the following information in my htaccess file:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site.2014/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site.2014/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(/)?$ site.2014/ [L]
You can use this code:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site.2014/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site.2014/$1 [L]
Make sure to test in a new browser.

How can htaccess replace www for the current directory name

First
Is it possible to stablish a rewrite rule so that when I type in the url bar
blog.mywebsite.com/file
it redirects to
www.mywebsite.com/blog/file.php?
The same if i wanted to go to the store directory of the site. Could I have a rule to redirect
store.mywebsite.com/file
to
www.mywebsite.com/store/file.php?
Second
Provided first part is ok, how could I do it to redirect this
blog.mywebsite.com/file/this-is-my-article
to
www.mywebsite.com/blog/file.php?url=this-is-my-article
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^[^.]+\.mywebsite\.com$ [NC]
RewriteRule ^/?$ /%1/file.php? [L,R=302]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^[^.]+\.mywebsite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /%1/file.php?url=$1 [QSA,L,R=302]

.htaccess subdomain traffic to folder and sub folders

Ok long story short:
http://subdomain.domain.com/test1/test2
needs to display the information here:
http://domain.com/agents/subdomain/test1/test2
and obviously if you with this
http://subdomain.domain.com/
needs to display
http://domain.com/agents/subdomain/
Here's what I have so far:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC]
RewriteCond %{HTTP_HOST} ([^\.]+)\.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/agents
RewriteRule ^(.*)$ /agents/%1/$1/ [L]
I need the first rule to get rid of the index.php from the URL.
I can get the first piece done http://subdomain.domain.com/ but not the sub folders.
the above syntax is throwing an internal server error but if I change the last line to:
RewriteRule ^$ /agents/%1/$1/ [L]
it works except I can't get to sub directories.
Ok finally figured this out... here's the solution:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)? domain\.com [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^(www\.)? domain\.com [NC]
RewriteCond %{HTTP_HOST} ([^\.]+)\. domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^/?(.*) /index.php?/agents/%1/$1 [L]

Resources