Always WWW modrewrite php - php

I want to force www in my urls. I'm having problems with writing. I know that this code will do the trick.
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1
But I want to put all in $_GET['page']
RewriteRule ^(.*)$ /index.php?page=$1 [L]
How should I put this together?

You need to add [QSA], which allows you to modify the query string of the URL:
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]

this should do the job
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php?page=$0 [L]

Related

htaccess rewrite based on domain

my project has multiple domains and I would like to redirect 100 pages for 1 of these domain.
Now I found this post on how to set up conditions for 1 domain.
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC]
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA]
My question would be, how can I set this up for 100 pages?
Do I have to duplicate the condition for each link?
What I want to do is this
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^some https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings https://www.mypage.de/shop/settings [R=301, L]
There are so many scenarios to do in this case , for example , you could redirect every page except specific page like this :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/(page1|page2|page3|whatever)
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
But , if the number of page who should be excluded is large , you could create directory , for example , new then move all files that have to be redirected at it , then make this rule
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI} -f
RewriteRule ^(.*)$ /new/$1 [L]
Then you could add this to redirect any URI contains new to that new location :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} ^/new/
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
So code should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI} -f
RewriteRule ^(.*)$ /new/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} ^/new/
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
Assuming your page names are same after redirect as you've shown in your question, you can use a single rule like this as your top most rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.site1\.com$ [NC]
RewriteRule .+ https://www.mypage.de/shop/$0 [R=301,NE,L]
Make sure to use a new browser for your testing.
Probably Skip flag will be the best aproach.
It looks like this:
RewriteCond %{HTTP_HOST} !^www\.site1\.com [NC]
RewriteRule .? - [S=100]
RewriteRule ^some https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings https://www.mypage.de/shop/settings [R=301, L]
.....
RewriteRule ^rule_for_100nd_page https://www.mypage.de/shop/settings [R=301, L]
How this works.
Test HOST if NOT skip 100 (S=100) the next RewriteRules if YES RewriteRule .? - [S=100] is ignored
Write your 100 rules for www.site1.com source domain and go on.
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [QSA]
This will redirect every single page.
If you only want 100 specific pages you must add one by one. Like other answers were provided.
So after testing some of the suggestions I ended up having to add the condition to each rule in order to make it work.
So as far as I can tell you can't set conditions for more than one rule if the targets are to specific.
So my solution for this issue looks like this.
Thanks for all the suggestions.
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^some https://www.site1.de/shop/some [R=301, L]
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^page https://www.site1.de/random/page [R=301, L]
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^settings https://www.site1.de/whatever/settings [R=301, L]

mod_rewrite GET form to clean URL

I have tried searching but only came up with this link which I can get to work, just not correctly to apply to my current situation.
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1? [R=301,L,QSA]
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
RewriteRule ^highscores/personal/(.*) personal.php?user1=$1 [L,QSA]
RewriteRule ^highscores/skill/(.*) highscores.php?skill=$1 [L,QSA]
RewriteRule ^highscores/(.*) highscores.php?$1 [L,QSA]
As of right now, it will redirect
http://localhost/highscores/personal/?user1=test
to
http://localhost/highscores/personal/test
like it should.
But I have a compare function which submits a GET request like:
http://localhost/highscores/personal/?user1=test&user2=test2
which needs to come out like
http://localhost/highscores/personal/test/vs/test2
but it comes out like
http://localhost/highscores/personal/test&user2=test2
Edit: Resolved using help from this htaccess tester
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=([^&]+)&user2=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1/vs/%2? [R=301,L]
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1 [R=301,L]
You need to edit your rule
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
to
RewriteRule ^highscores/personal/([^/]+)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
because of greedy regex. You need to break (.*) section if / is found

htaccess remove www and redirect to accessed url

I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks

How i can edit my htaccess file to let my site work with "www" only

My .htaccess file code is
DirectoryIndex router.php
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^(.*)$
RewriteRule ^(.*)$ router.php?_doroute=$1 [L,QSA]
I want to let my site work with "www" only
i couldn't integrate this code with it
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone help me please ?
What is the full output of your .htaccess file? You are showing a rule of RewriteRule ^(.*)$ router.php?_doroute=$1 [L,QSA] which indicates stop processing at the end of that rule, [L].
If you are just adding:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
then that would be your issue.
An alternative method is:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]
Just add the folowing to your .htaccess
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Source:
LetUsLook.nl - Redirect non-www to www

properly structuring an htaccess with multiple rules/conds

How would i properly structure this htaccess file as to correctly function in order and avoid infinite loops?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
RewriteBase /
RewriteRule ^deviceToken/$ devicetoken.php [QSA,L]
RewriteRule ^register/$ register.php [QSA,L]
RewriteRule ^resetPassword/$ resetpassword.php [QSA,L]
RewriteRule ^deleteLink/$ deletelink.php [QSA,L]
RewriteRule ^getLinks/$ getlinks.php [QSA,L]
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)|(\.swf)|(\.xpi)|(\.ico)|(\.src)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule (.*)$ get.php?code=$1 [L]
It looks OK, have you tried it already?
This line is useless:
RewriteCond %{REQUEST_URI} ^(.*)$
Redirect domain first:
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
(added ,L)

Categories