htaccess not working for www and non-www - php

i'm trying to redirect www.site.ru and site.ru to www.site.ru/ru_RU. But i can't make it work.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's not redirecting WWW version. Can someone tell me how to make that request. By the way, sometimes i come to situation, where in firefox it's working, but in IE it's not.

Try this code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?site\.ru$ [NC]
RewriteRule ^$ http://www.site.ru/ru_RU [L,R]
## WHAT IS THIS RULE DOING??
# RewriteCond %{REQUEST_URI} ^/news
# RewriteRule ^ /news [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
btw I had to comment out suspicious looking 2nd rule for /news. If you can explain what you're trying to do with this I can suggest you how to fix it.

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ru_RU [R=permanent,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ru_RU [R=301,L]

Related

Htaccess RewriteRule add HTTP/1.1 at url end

I need this url:
www.99bankinfo.in/index.php?bank=AxisBank
to
www.99bankinfo.in/AxisBank
but it appears this:
www.99bankinfo.in/AxisBank%20HTTP/1.1/
My .htaccess Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)
RewriteRule ^ /%2/? [L,R=301]
Kindly suggest any solution, i am stucked for days...
Thanks in advance.
You need to tweak regex in last redirect rule:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&\s]+)
RewriteRule ^ /%2/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Also keep redirect rules before rewrite ones.

.htaccess rewrite not working 301redirect

I have the following code in my .htaccess to rewrite the username.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L]
</IfModule>
So this is the original url:
www.domain.com/profile.php?username='Mike'
Into this:
www.domain.com/Mike
Now I have another rewrite rule for www 301 redirect.
This also works great except when I'm at wwww.domain.com/Mike and I delete the 'www' and this is what I'm getting in my url:
www.domain.com/profile.php?username=Mike
My question is why im not getting the same url:(www.domain.com/Mike) after deleting 'www'?
Here is my full code in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
Change order of your rules to keep 301 redirect rules before internal rewrite ones:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L,QSA]

Using .htaccess to rewrite dynamic subdomains

I'm currently using .htaccess to rewrite on my website to create dynamic subdomains. I also use it to remove the .php extension on all pages. However, once inside the subdomain, it tries to redirect again. For example, if you went to https://admin.example.com/test, it would actually be accessing https://example.com/clients/admin/test.php. I keeping getting various 404 errors using the following .htaccess file:
Options +MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
How can I keep this from redirecting to https://admin.example.com/clients/admin/test.php?
First, you probably want to turn off multiviews, that's going to mess with the whole "removing the php extension" thing
Than, you want this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To be after your redirects, and you want to be doing the redirect status check on the actual redirects:
Options -Multiviews + FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/clients/
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I want to redirect domain.com/index.php to domain.com

How to redirect mydomainname.com/index.php to mydomainname.com
Currently I use following codes also.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mydomianname\.com
RewriteRule (.*) http://mydomianname.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule>
This should be your complete .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mydomianname\.com
RewriteRule (.*) http://mydomianname.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule>
This example only works for http, but should roughly do the trick.
# Redirect requests with index.php to the corresponding request
# without the index.php
RewriteRule ^index.php(.*) http://mydomianname.com$1 [R=301,L]

How do I redirect to a page of my site taking the trailing slash as the language parameter using .htaccess

I have this rule which is not redirecting properly:
#redirect from www.mysite.com/page-NAME/es to www.mysite.com/public/index.php?page=NAME/es
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule page-(.*)/es$ public/index.php?page=$1&lang=es [L]
Internally the parameter page is not getting received so the site shows the home page instead. I am not sure if I have this rule wrong or if it is just conflicting with the other rules which are working perfectly, or if it is an order problem.
All help is greatly appreciated.
Thanks in advance.
Below is the full htaccess file on the root of my site, in case you want to take a look at it.
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php
#Adds www to the domain
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
#redirect from www.mysite.com/es to www.mysite.com/public/index.php?lang=es
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule /es$ public/index.php?lang=es [L]
#redirect from www.mysite.com/en to www.mysite.com/public/index.php?lang=en
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule /en$ public/index.php?lang=en [L]
#redirect from www.mysite.com/ to www.mysite.com/public/index.php
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(/)?$ public/index.php [L]
#redirect from www.mysite.com/page-NAME/es to www.mysite.com/public/index.php?page=NAME/es
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule page-(.*)/es$ public/index.php?page=$1&lang=es [L]
#redirect from www.mysite.com/page-NAME to www.mysite.com/public/index.php?page=NAME
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule page-(.*)$ public/index.php?page=$1 [L]
#redirect from www.mysite.com/ to www.mysite.com/public/
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
ok found the problem seems, it was indeed an order issue, this is way seems to work, hopefully this helps someone with the same issue
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php
#Adds www to the domain
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
#redirect from www.mysite.com/page-NAME/es to www.mysite.com/public/index.php?page=NAME/es
RewriteRule page-(.*)/es$ public/index.php?page=$1&lang=es [L]
#redirect from www.mysite.com/page-NAME to www.mysite.com/public/index.php?page=NAME
RewriteRule page-(.*)$ public/index.php?page=$1 [L]
#redirect from www.mysite.com/es to www.mysite.com/public/index.php?lang=es
RewriteRule /es public/index.php?lang=es [L]
#redirect from www.mysite.com/en to www.mysite.com/public/index.php?lang=en
RewriteRule /en public/index.php?lang=en [L]
#redirect from www.mysite.com/ to www.mysite.com/public/index.php
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(/)?$ public/index.php [L]
#redirect from www.mysite.com/ to www.mysite.com/public/
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]

Categories