how capture subdomains in rewrite rules - .htaccess

I want redirect this urls:
http://tops.domain.com/test
http://files.domain.com/css/style.css
http://domain.com/test
to these:
/tops.php?what=test
/css/style.css
/index.php?username=test
I written this codes in .htaccess file and put it in root:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^tops\. [NC]
RewriteRule .* tops.php?what=$0 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^files\. [NC]
RewriteRule .* $0 [NC,L,QSA]
RewriteRule .+ index.php?username=$0 [L,QSA]
but not work right

Use this .htaccess :
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^files\. [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^tops\. [NC]
RewriteCond %{REQUEST_URI} !^/tops\.php [NC]
RewriteRule .* tops.php?what=$0 [QSA,L,NC]
RewriteCond %{QUERY_STRING} !^username= [NC]
RewriteRule .+ index.php?username=$0 [L,QSA]

Related

Mod_rewrite http->https redirect EXCEPT one URL

OK, so I'm trying to do this:
!www -> www
http -> https
EXCEPT
/[a-f0-9]{11}/
https://www.domain.com/[a-f0-9]{11}/ -> http://www.domain.com/[a-f0-9]{11}/
map to controller: index.php?(uri)
e.g. /controller/action/id -> index.php/controller/action/id
Summary: all URLs should be https://www.domain.com/(.*) except /[a-f0-9]{11}/, which should be forced to http only.
I have an old set of rules (see below) which don't look very clean.
I've tried adding to them to account for /[a-f0-9]{11} and it ends up redirecting instead of remapping, so I end up with www.domain.com/index.php/thepattern0
How do I clean these rules up and make this work?
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
#redirect to www
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
#redirect to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#hide index.php
RedirectMatch 404 .*php\.ini
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
You can use:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
#redirect to www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
#redirect to http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
#hide index.php
RewriteRule \.(?:php|ini)$ [L,R=404,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

.htaccess RewriteRule url to new

Bonjour,
I try to redirect urls of a website
Example :
www.exemple.net/?p=2
to
www.exemple.net/index-2.html
www.exemple.net/?p=35
to
www.exemple.net/index-35.html
etc...
So i add this lines to my .htacess :
RewriteCond %{QUERY_STRING} ^(?)p=(.*)$ [NC]
RewriteRule .* /index-%1.html [L,R=301]
But i'm redirected to http://www.exemple.net/index-2.html?p=2
My .htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^exemple.net$
RewriteRule ^(.*) http://www.exemple.net/$1 [QSA,L,R=301]
RewriteCond %{QUERY_STRING} ^(?)p=(.*)$ [NC]
RewriteRule .* /index-%1.html [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Can you help me please ?
You need to add a question mark at the end of the destination to discard the query string:
RewriteRule .* /index-%1.html? [L,R=301]
Alternatively, use the QSD flag:
RewriteRule .* /index-%1.html [QSD,L,R=301]
Try :
#Redirecting /?p=2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/\?p=([^&\s]+) [NC]
RewriteRule ^ /index-%1.html? [NE,NC,R,L]
#Redirecting /transfer?p=2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/transfer\?p=([^&\s]+) [NC]
RewriteRule ^ /transfer-%1.html? [NE,NC,R,L]
#Redirecting /blog?p=2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/blog\?p=([^&\s]+) [NC]
RewriteRule ^ /blog-%1.html? [NE,NC,R,L]

Getting value with htaccess and pagination

This url
example.com/videos/load.php?cat=video-category
would become
example.com/videos/video-category
Also, there is pagination url
example.com/videos/load.php?cat=video-category&p=2
and it would become
example.com/videos/video-category/page/2
I tried to do this.. but it doesn't work.
Here is my htaccess (into videos folder)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^(\w+/+/)page/?$ $1 [R=301,L]
RewriteRule ^(\w+)(?:/[^/]+)?/?$ load.php?cat=$1 [L,QSA]
RewriteRule ^(\w+)/[^/]+/([0-9]+)/?$ load.php?cat=$1&p=$2 [L,QSA]
Put this code in your htaccess.
This should work
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/page/?$ $1 [R=301,L]
RewriteRule ^([^/]+)/page/([1-9][0-9]*)$ load.php?cat=$1&p=$2 [L,QSA]

.htaccess not redirecting where I expect

1- I wrote the following redirect expecting that when this url
"http://www.curtainrodstore.com/index.php?crn=234"
was entered it would redirect to
"http://www.curtainrodstore.com/product-category/curtains-drapery/shower-curtains/"
2- Instead it redirects to
"http://www.curtainrodstore.com/?crn=243"
3- Note the missing
"index.php"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^curtainrodstore.com [NC]
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} crn=243
RewriteRule ^.*$ http://www.curtainrodstore.com/product-category/curtains-drapery/shower-curtains/ [R=301,L]
</IfModule mod>
Does anyone have any suggestions as to why it is not working?
Here is an updated attempt list…
<IfModule mod_rewrite.c>
RewriteEngine On
#ATTEMPT 1
#RewriteCond %{HTTP_HOST} ^curtainrodstore.com [NC]
#RewriteCond %{REQUEST_FILENAME} index\.php
#RewriteCond %{QUERY_STRING} crn=243
#RewriteRule ^.*$ http://www.curtainrodstore.com/product-category/curtains-drapery/shower-curtains/ [R=301,L]
#ATTEMPT 2
#RewriteRule ^index\.php\?crn=243$ http://curtainrodstore.com/product-category/curtains-drapery/shower-curtains/ [R=301,L]
#ATTEMPT 3
#RewriteCond %{HTTP_HOST} ^www.curtainrodstore.com$ [NC]
#RewriteCond %{QUERY_STRING} ^crn=234$
#RewriteRule ^index\.php$ http://www.curtainrodstore.com/product-category/curtains-drapery/shower-curtains/? [R=301,L]
#ATTEMPT 4
#RewriteCond %{HTTP_HOST} ^curtainrodstore.com$ [NC]
#RewriteCond %{QUERY_STRING} ^crn=234$
#RewriteRule ^index\.php$ http://curtainrodstore.com/product-category/curtains-drapery/shower-curtains/? [R=301,L]
#ATTEMPT 5
#
</IfModule>
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?curtainrodstore.com [NC]
RewriteCond %{REQUEST_URI} index\.php [NC]
RewriteCond %{QUERY_STRING} crn=234 [NC]
RewriteRule .* product-category/curtains-drapery/shower-curtains/? [R=301,L]
Redirects permanently
http://www.curtainrodstore.com/index.php?crn=234
To:
http://www.curtainrodstore.com/product-category/curtains-drapery/shower-curtains/
For silent mapping, remove R=301 from [R=301,L]
Make sure browser's cache is cleared before any test

Wrong 301 htaccess redirection

I have this code on my site and I need to redirect the http://softsolutions.fr to http://www.softsolutions.fr, but it is not redirecting:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options +Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^softsolutions\.fr
RewriteRule ^(.*)$ http://www.softsolutions.fr/index.html [R=301,L]
RewriteRule ^$ /index.html [L]
# Rewrites "sub.domain.foo/anything" to "sub.domain.foo/anything.php"
RewriteCond %{REQUEST_FILENAME} !^(.+).php$
RewriteCond %{REQUEST_FILENAME} !^(.+).pdf$
RewriteCond %{REQUEST_FILENAME} !^(.+).(html|htm)$
RewriteRule ^([a-zA-Z0-9\-\_/]*)$ /$1.php [L]
</IfModule>
Replace those lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^softsolutions\.fr
RewriteRule ^(.*)$ http://www.softsolutions.fr/index.html [R=301,L]
With:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^softsolutions\.fr [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI}

Resources