Mod_rewrite: hide query string from users with sub domain - .htaccess

I am trying to do something very simple yet still confusing.
I have a link
http://sub.example.com/folder/?qvar=value
I want this to be accessible via:
http://sub.example.com/folder/value
Very simple yet I cannot figure this out.
I have tried the following to no avail:
#RewriteEngine On
#RewriteRule ^(.*)$ ?qvar=$1 [L, QSA]
#RewriteRule ^(.*)$ /folder/index.php?qvar=$1 [L,QSA]
#RewriteRule ^(.*)$ index.php?qvar=$1 [L,QSA]
#RewriteRule ^(.*)$ /folder/?qvar=$1 [L,QSA]
#RewriteRule ([^/\.]+)/?$ index.php?qvar=$1 [L,QSA]
#RewriteCond %{QUERY_STRING} qvar=(.*)
#RewriteRule index.php %1 [L]
Some of these give me a 500 internal server error.
Others redirect me to /sub/sub/folder/.
Please help!

Try:
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /folder/\?qvar=([^&\ ]+)
RewriteRule ^ /folder/%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^folder/(.*)$ /folder/?qvar=$1 [L,QSA]
These rules need to be in the htaccess file in your document root

Related

htaccess - Remove Query String from URL but Leave Value

I have searched all over Stackoverflow and Googled wide and far, but I cannot get this seemingly simple task done.
I'm trying to set a rule in htaccess to clean the URL by removing the query string, but leaving its value intact:
http://example.local/?p=subscribe
Becomes:
http://example.local/subscribe
I have tried these various methods:
RewriteEngine on
RewriteCond %{QUERY_STRING}
RewriteRule (.*) /$1? [R=301,L]
or,
RewriteCond %{QUERY_STRING} ^p=$ [NC]
RewriteRule ^(/?)?$ $1? [R=301,L,NC]
or,
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+)
RewriteRule ^$ /%1? [R=301,L]
But nothing works!
Any help will be greatly appreciated.
Have it like this in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/([^?]*?)/?\?p=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ ?p=$1 [L,QSA]
You can use % to retrieve a rewriteCond Variables
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^(.*)?$ %1? [R=301,L,NC]
Will convert any url http:/site.com/url?qs to http:/site.com/qs

www to non-www with SSL without index.php

When I try to enter my website from url www.mypage.com I got something like http://www.mypage.com/index.php/https://mypage.com/https://www.mypage.com....
When I enter from url mypage.com I got https://mypage.com - AND ITS OK
When I enter from url mypage.com/stuff I got https://mypage.com/index.php/stuff (page is working)
When I enter from url www.mypage.com/stuff I got https://mypage.com/index.php/stuff
What I should do? I would like to have always url like https://mypage.com without index.php and without 'www'.
My .htaccess :
AddHandler application/x-httpd-php70 php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Thanks!
Try switching the order around so the internal redirect to index.php/$1 executes last:
AddHandler application/x-httpd-php70 php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Htaccess same rule for 2 files not working

I am trying to create friendly pagination using htaccess file.
But its not working I guess I am using wrong rule for files.
Check out my codes below.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/$ cat.php?id=$1 [NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]
Everything is working fine, only cat2.php is not working.
How to fix it?
You need to switch the order around between your 2 cat rules. (.*) matches everything, including slashes, so it will always match whatever cat2 matches. Try just swapping their orders and include L flags:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [L,NC,QSA]
RewriteRule ^(.*)/$ cat.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]

htaccess 301 redirect everything except /admin

I have been scratching my head for almost half an hour already because of this. I don't know what I am doing wrong, might just be under my nose, but i just cant see it. Here's what I have on my .htaccess
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
When I access example.com/admin, i am still redirected to newsite.com/admin.
Can anyone please advise? Thanks!
UPDATE: This is my everything that I have on mod_rewrite currently:
RewriteRule ^googleabcdef12345.html - [L]
RewriteCond %{HTTP_HOST} ^sample1\.no$ [NC]
RewriteRule ^(.*)$ http://www.newsample1.no/$1 [L,R=301]
RewriteCond %{HTTP_HOST} example\.com$ [NC]
#RewriteRule admin - [S=1]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It is a drupal website btw. I tried QUERY_STRING instead of REQUEST_URI but no luck still.

htaccess not working properly

My current htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2&subid=$3
I've edited the htaccess file. It should only use this htaccess for the main domain and not for subdomains, however subdomains still use this htaccess.
Anyone with more experience than me could point me out in the right direction? Thanks in advance.
Edit:
Here is my current (and working) htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2&subid=$3
Try this:
RewriteEngine On
# first, remove redirect to www by default if no subdomain
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&subsectie=$2 [NC,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&subsectie=$2&subid=$3 [NC,L]

Resources