redirect to www. without lost variables get - php

I am redirecting via .htaccess so that when they enter my domain.com site it will take them to www.domain.com. This works fine, but when the url contains some get variable it gets lost.
Example:
Entering domain.com/archive.php?var=something.
I get redirected to www.domain.com/archive.php
Losing the get variable var=something...
In my htaccess I have the following:
RewriteRule (.) http://www.example.com/$1 [R=301,L]
#redirigir index.html e index.php a raiz
RewriteCond %{REQUEST_URI} ^/index.php
RewriteRule ^.$ http://%{HTTP_HOST} [R=301,L]
RewriteCond %{REQUEST_URI} ^/index.html
RewriteRule ^.*$ http://%{HTTP_HOST} [R=301,L]
What am I doing wrong?

Hey i think this can work for you
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://www.domainB.com/$1 [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.domainB.com/$1 [R=301,L]

Related

Htaccess multiple redirect rules should be working

This should be working according to htaccess tester http://htaccess.madewithlove.be/
I'm trying to get url's in the format subdomain.domain.com to resolve to domain.com/index.php?sub=subdomain
However I've also want any links in the form subdomain.domain.com/pagename to redirect to domain.com/index.php?tpl=page&sub=subdomain&url=pagename
At the moment the first rule works if i remove the second rule but if I include both only the second rule works.
Here's the full htaccess
RewriteEngine On
#EDIT: this was messing it all up by appending index.html so the subdomain only
# wasn't triggering at all due to appended pagename
DirectoryIndex disabled
#Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/index.php?sub=%1 [P,NC,QSA,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteRule ^(.+/)?([^/]*)$ http://example.com/index.php?tpl=page&sub=%1&url=$2 [P,NC,QSA,L]
If you answer this I will make you Secretary of State once Flat Earth goes mainstream. Thanks!
Have it like this:
DirectoryIndex disabled
RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ index.php [L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^index\.php$ http://%2/index.php?sub=%1 [P,QSA,NC,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^(?:.+/)?([^/]+)/?$ http://%2/index.php?tpl=page&sub=%1&url=$1 [P,QSA,L]

htaccess by adding index.php in the root of the site

This is my current htaccess:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $1 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^domain.com.br$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
Options -Indexes
In the first block lines it redirects the http to https;
In the second block it allows some direct access and removes the index.php
In the third block it adds the www if it does not exist.
The ultimate goal is to have urls like:
https://www.domain.com.br ou
https://www.domain.com.br/foo/bar
The problem is that when the user enters the address
Domain.com.br (without www and without the http, only in the root of the site) it redirects to:
https://www.domain.com.br/index.php
Only at the root of the site does it add this index.php
Does anyone know where I'm going wrong?
I accept htaccess optimization suggestions as well
You can use these refactored rules:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $0 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt) [NC]
RewriteRule .* index.php/$0 [L]
Test it after completely clearing your browser cache.

How can I redirect www without moving to the homepage?

Well I'm using this code to redirect the domain and ip:
RewriteEngine On
rewritecond %{http_host} ^www.lucrebem.com.br [nc]
rewriterule ^(.*)$ http://lucrebem.com.br/$1 [r=301,nc]
RewriteRule ^(.*)['"$] /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^216\.245\.194\.194
RewriteRule (.*) http://lucrebem.com.br/$1 [R=301,L]
What it does is removing the www from the url, but the problem is, if an user hits www.mysite.com/thecategory/thearticle, he won't be redirected to mysite.com/thecategory/thearticle, he will be redirected to mysite.com/index.php
That's not what I'm trying to achieve and I believe this is affecting my rankings.
I would do it this way and you can combine you rules into one instead of separate rules.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.lucrebem\.com\.br [NC,OR]
RewriteCond %{HTTP_HOST} ^216\.245\.194\.194
RewriteRule ^ http://lucrebem.com.br%{REQUEST_URI} [R=301,L]
Here you go Buddy, this should remove the www and redirect it to domain + slugs:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
or
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Here is how I fixed it:
### Makes www Redirect to non-www without redirecting to index page
RewriteCond %{HTTP_HOST} ^www\.lucrebem\.com\.br
RewriteRule (.*) http://lucrebem.com.br/$1 [R=301,L]
And here is a Bonus for people who wants to redirect the Ip too:
#Redirects IP, working fine, don't change anything below
RewriteCond %{HTTP_HOST} ^216\.245\.194\.194
RewriteRule (.*) http://lucrebem.com.br/$1 [R=301,L]
:)

rewrite from http to https results on index.php for any request

I'm using this htaccess code. I expect any call to be directed to https://www.mysite.com/theme/topic. I've really looked lots of links & helps - the result is only and ever:
https://www.example.com/index.php
so the /theme/topic are not in the output url, only the index.php
what is wrong here? thanks for any help
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### If file exists, use it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,QSA,R=301]
</IfModule>
you say it yourself:
so the /theme/topic are not in the output url, only the index.php
try to change the line
RewriteRule ^(.*)$ index.php [L]
into
RewriteRule ^(.*)$ /theme/topic/$1 [L]
Or is there a specific reason why not to put it there?
[edit after clarification]
So you have 2 dynamic parameters and want to rewrite yourdomain.com/parameter1/parameter2 to yourdomain.com/index.php?p1=parameter1&p2=parameter2? Then try the following:
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
[re-edit after more clarification]
Oh, so it is just redirecting from http to https and redirecting to www? Then you can just do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC, OR]
RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://www.mysite.com/%{REQUEST_URI} [R=301,L]
</IfModule>

How to delete a subdirectory from a URL?

So I'm trying to turn my website from this:
http://profe5.com/Profe5-Web/public_html/login.html
To this:
http://profe5.com/login
I've been struggling to do this, but whenever I run it I get 404 error!
This is my htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [R=301,L]
RewriteRule ^Profe5-Web/public_html/(.*)$ $1 [R=301,L]
It would be so awesome if you guys could help me!
Thanks so much!
This should be your complete .htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Profe5-Web/public_html/([^.]+)\.html [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ Profe5-Web/public_html/$1.html [L,QSA]
you might consider instead doing this via a virtual host; check with your hosting company about setting it up
If you want /login to map to Profe5-Web/public_html/login.html - try this:
RewriteRule ^([^.]+)$ Profe5-Web/public_html/$1.html [R=301,L]
The RewriteRule you already have, RewriteRule ^([^\.]+)$ $1.html [R=301,L] will map /login to /login.html, which doesn't seem to do what you need it to.

Categories