.htaccess redirect losing query string? - apache

I have a .htaccess that looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^.]+)/?$ index.php?query=$1 [L]
I was hoping that would make all non-www urls redirect to the www version, as well as giving a tidy query string on the end. So these urls:
http://site.com/index.php?query=test/page
http://www.site.com/index.php?query=another/test/page
Would both redirect to:
http://www.site.com/test/page
http://www.site.com/another/test/page
Respectively.
However, everything is just redirecting to:
http://www.site.com
What am I doing wrong?

RewriteRule ignores the query string in the url. So you will need to change your rules as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^/index[.]php$ /%{QUERY_STRING} [L]
Make sure you include the QSA flag on the first rule

Related

Apache redirect using RewriteEngine with and without query string

I'm trying to get 2 redirections set on my htaccess file but I can't make it to work.
The desired output is:
if the user goes to www.mywebsite.com/page1, it gets redirected to /folder/newPage
if the user goes to www.mywebsite.com/page1?a=123, it gets redirected to /folder/anotherPage?a=123
I tried this
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/page1(.*)$ /folder/anotherPage$1 [R=301,L]
I also tried something like this:
RewriteCond %{REQUEST_URI} ^/page1$
RewriteRule ^(.*)$ /folder/newPage [R=301,L]
RewriteCond %{REQUEST_URI} ^/page1$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ /folder/anotherPage%1 [R=301,L]
Note that Rewrite mod is turned on on my apache.
Any idea how to make it work?
Check through the following:
RewriteEngine On is present before all the rules
AllowOverride directive allows your htaccess files to load
htaccess receives URLs without the leading slashes, unlike server config files, or VHosts file.
.* matches 0 or more characters, thus %{QUERY_STRING} ^(.*)$ also matches empty query strings. Change this to .+.
Final rules should be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^page1$ /folder/anotherPage?%1 [R=301,L]

rewrite www. to without www. in .htaccess for hostname instead of writing in the website name?

I currently use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.e-innovate.co.uk [NC]
RewriteRule ^(.*)$ http://e-innovate.co.uk/$1 [L,R=301,NC]
But what i am trying to do is create a general rewrite code which i can drop into .htaccess files without having to change the domain name such as below, i dont seem to be able to get this to work, am i on the right track ?
Thanks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
note that this will also get applied to subdomains.

Mod Rewrite with multiple parameters

I'm trying to setup a rewrite rule so that any text in place of a subdomain will be parameter one and any text after the first forward slash will be parameter two but I'm struggling with regex and am unsure about rewrite terminology.
For example, if someone requested:
joebloggs.mydomain.com
I would like them to see:
mydomain.com/index.php?site=joebloggs
Also, if someone requested:
joebloggs.mydomain.com/contact
I would like them to see:
mydomain.com/index.php?site=joebloggs&page=contact
By "see" I mean see the page, rather than see the URL - it's a CMS-like project so you can probably see where I'm going with it. Also, I've worked out how to remove www. so that's not an issue :)
EDIT
Current .htaccess is:
RewriteEngine On
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [L]
# Remove www
RewriteCond %{HTTP_HOST} ^www.richardmjenkins.com$ [NC]
RewriteRule ^(.*)$ http://richardmjenkins.com/$1 [R=301,L]
# Rewrite for site/page pair
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^$ p.php?s=%1 [QSA,L,NC]
1. In your host panel : add a subdomain with name : * for enable all subdomains
2. this is your htaccess code :
RewriteEngine On
Options +Followsymlinks
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^$ index.php?site=%1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?site=%1&page=$1 [QSA,L,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^$ index.php?site=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^(.+)$ index.php?site=%1&page=%2 [L,QSA]

.htaccess Exclude directory in rewrite

I want to exclude URL rewriting if the directory is mysite.com/admin. I have tried:
# prepend http://www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(admin|user)($|/) - [L]
I'm not too familiar with this stuff, but I can't seem to get this working. Does anyone see the solution?
Try moving the pass-through for the admin | user directories before the redirect:
RewriteEngine On
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Other than that, I don't see any rewriting going on at all, so I'm not sure what they need to be excluded from.

Redirect not working for complex rewrite rules (.htaccess)

I'm trying to make a redirect from a non-www version of the link to the www one. Its working fine for something like http://mywebsite.com but it fails for a request like http://mywebsite.com/artists/metallica/ or even a complex one. The whole .htaccess file is bellow. Any clues?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]
RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L]
RewriteRule ^submit/$ /submit.php [QSA,L]
RewriteRule ^users/$ /users.php [QSA,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But make sure that you put this rule in front of those rules that just do an internal rewrite. Otherwise an already internally rewritten rule might get redirected externally.