Multiple RewriteRules for single RewriteCond - php

i have this code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com:8080
RewriteRule ^ http://www.domain.com:8080%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?\.html$ view.php?prefix=%1&cat=$1&id=$2&title=$3 [L,QSA]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ categories.php?prefix=%1&cat=$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^(index\.html)/?$ category_index.php?prefix=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com:8080 [NC]
RewriteRule .? - [S=3]
RewriteRule ^(tube|login|register|facebook|logout)\.html$ $1.php [L,QSA]
RewriteRule ^page/([A-Za-z0-9-]+).html$ page.php?prefix=$1
RewriteRule ^sitemap\.xml sitemap.php [QSA,L]
</IfModule>
i want login.html and all sisters open only on www.domain.com/login.html and give 404 error if opened on games.domain.com/login.html
and also RewriteRule .? - [S=3] not work for me, i'm tried to do
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule .? - [S=3]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?\.html$ view.php?prefix=%1&cat=$1&id=$2&title=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ categories.php?prefix=%1&cat=$1 [L,QSA]
RewriteRule ^(index\.html)/?$ category_index.php?prefix=%1 [L,QSA]
its not working here and here
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com:8080 [NC]
RewriteRule .? - [S=3]
RewriteRule ^(tube|login|register|facebook|logout)\.html$ $1.php [L,QSA]

This condition is wrong:
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080 [NC]
As you can only match host name using HTTP_HOST variable without port. To make it work use this compound condition:
RewriteCond %{HTTP_HOST}:%{SERVER_PORT} !^(www\.)?domain\.com:8080$ [NC]

Related

How to Force domain to with www

this is my htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
but my site loaded with non www.
Set condition to any address not starting with www and then redirect.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

add new rule to my htaccess file to replace ? by /

here is my htaccess file please take a look
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+page\.php\?brand=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
### rules to convert ?n1=v1&n2=v2 /n1/v1/n2/v2
RewriteCond %{THE_REQUEST} /profile\.php\? [NC]
RewriteCond %{QUERY_STRING} ^([^&]+)&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]
RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^([^=]+)=(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]
RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^[^&=]+$
RewriteRule ^(profile)\.php$ /$1/%{QUERY_STRING}? [L,NE,R=302]
# recursion rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule ^(profile)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$ /$1.php$4?$2=$3 [L,QSA]
### end of convert ?n1=v1&n2=v2 /n1/v1/n2/v2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?brand=$1 [L,QSA]
I want to add a new rule to this file which can replace this url www.domain.com/profile?id=2 to www.domain.com/profile/2
i am new to htaccess please help me
and one important thing is that i should able to fetch the get variable fro id in my php code
$idis=$_GET['id'];
Your rules are getting pretty complicated. With that regex pattern and ordering has become much more important. Have your full .htaccess as this:
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+page\.php\?brand=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [L,R=302]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
### rules to convert ?n1=v1&n2=v2 /n1/v1/n2/v2
RewriteCond %{THE_REQUEST} /profile\.php\? [NC]
RewriteCond %{QUERY_STRING} ^([^&]+)&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]
RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^([^=]+)=(.*)$
RewriteRule ^ %{REQUEST_URI}?%1/%2 [DPI,E=QS:1]
RewriteCond %{ENV:REDIRECT_QS} =1
RewriteCond %{QUERY_STRING} ^[^&=]+$
RewriteRule ^(profile)\.php$ /$1/%{QUERY_STRING}? [L,NE,R=302]
# recursion rule to replace /n1/v1/n2/v2 to QUERY_STRING
RewriteRule ^(profile)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$ /$1.php$4?$2=$3 [L,QSA]
### end of convert ?n1=v1&n2=v2 /n1/v1/n2/v2
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [QSA,NC,L]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^(.+)$ page.php?brand=$1 [L,QSA]
Try:
RewriteCond %{THE_REQUEST} /profile\?id=([^\s]+) [NC]
RewriteRule ^ /profile/id/%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/id/([^/]+)/?$ /profile/$1 [NC,L]

Htaccess rewrite match same patterns

How can I make both rewrites work like http://example.com/something.html http://example.com/videos/something/1.html it always matches the download.php one but not the video.php.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=$1
RewriteRule ^videos/(.+)/(.+).html$ video.php?q=$1&page=$2
RewriteRule ^(.+).html$ download.php?id=$1
You can have these rules as:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^category/(.+)$ category.php?q=$1 [L,QSA,NC]
RewriteRule ^videos/([^/]+)/([^/.]+)\.html$ video.php?q=$1&page=$2 [L,QSA,NC]
RewriteRule ^(.+)\.html$ download.php?id=$1 [L,QSA,NC]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteBase /
RewriteRule ^category/(.+)$ category.php?q=$1
RewriteRule ^videos/(.+?)/(.+?).html$ video.php?q=$1&page=$2
RewriteRule ^(.+).html$ download.php?id=$1
+ is a greedy operator. This is why download.php was always matched.

How to rewrite a subdomain with .htaccess?

I have a subdomain like this:
Example: http://us.example.com/index.php?city=newyork
How do I rewrite to:
http://us.example.com/newyork
"us" is a virtual subdomain. So, It can be different: us, fr, it, etc..
I tried in this way but it does not work
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) index.php?city=$1
Keep it like this:
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?city=$1 [L,QSA]
EDIT:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^&]+)&ountry=([^\s&]*)\s [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?city=$1&country=$2 [L,QSA]

Rewriting is not working but gives an internal error in .htaccess

RewriteEngine On
RewriteRule ^http://example.lk/example1/ln1/([^/]*)$ http://example.lk/example1?ln1=$1 [L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/example\.lk\/ln\/1" [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^contactus$ "http\:\/\/example\.lk\/example3\/cm\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example2$ "http\:\/\/example\.lk\/example2\/a\/\$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example4$ "http\:\/\/example\.lk\/example4\?\/vi\/\$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example6$ "http\:\/\/example\.lk\/example6\/id\/\$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example1$ "http\:\/\/example\.lk\/example1\/ln2\/\$1" [R=301,L]
As pointed out, there a number of errors, please read the manual.
You don't need to double quote or escape the redirect substitutions.
You don't need to match the ${HTTP_HOST} if you don't care what it is.
You can't have a $1 in the substitution without a matching group in the pattern.
I've tried to fix some of the problems... but you need to provide more information on what specifically doesn't work and what you're trying to achieve.
RewriteEngine On
RewriteRule ^example1/ln1/([^/]*)$ http://example.lk/example1?ln1=$1 [L]
# RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ http://example.lk/ln/1 [R=301,L]
# RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^contactus$ http://example.lk/example3/cm/ [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example2$ http://example.lk/example2/a/ [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example4$ http://example.lk/example4/vi/ [R=301,L]
#RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example6$ http://example.lk/example6/id/ [R=301,L]
#RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example1$ http://example.lk/example1/ln2/ [R=301,L]

Categories