How can I do the following in .htaccess? - php

I need to rewrite
site.com/blog.php?blogger=somebody to somebody.site.com
site.com/blog.php?blogger=somebody&content=sth&page=nxt to
somebody.site.com/sth/nxt
using .htaccess

Try this:
RewriteCond %{HTTP_HOST} ^site.com$
RewriteCond %{REQUEST_URI} ^/blog.php$
RewriteCond %{QUERY_STRING} ^blogger=(.*)$
RewriteRule .* http://%1.site.com [L,R=301]
RewriteCond %{HTTP_HOST} ^site.com$
RewriteCond %{REQUEST_URI} ^/blog.php$
RewriteCond %{QUERY_STRING} ^blogger=(.*)&content=(.*)&page=(.*)$
RewriteRule .* http://%1.site.com/%2/%3 [L,R=301]

Related

How to set up Apache mod_rewrite redirect rules?

I am not quite familiar with Apache settings. I need to make website loading sub-directory content except one page.
Currently got a website and need to make all calls to http://www.domain.com & http://domain.com load contents from http://www.domain.com/subfolder (but looks like http://www.domain.com)
Only except the http://www.domain.com/checkout page, this one page should redirect to https://www.domain.com/checkout for secure checkout
The current mod_rewrite shown as below:
RewriteEngine on
RewriteRule ^$ domain/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/domain%{REQUEST_URI} -f
RewriteRule .* domain/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* domain/index.php?q=$0 [QSA]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com.au$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.domain.com.au [R,NC]
Open the file named .htaccess in the root of your webserver and add following lines of code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]
rewrite for your complete .htaccess-file (check if this works, then I'll delete the previous code):
RewriteRule ^$ subfolder/index.php [QSA,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.yourdomain.com [NC]
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^subfolder/(.*) /subfolder/index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]

Rewrite domain using htaccess and remove subfolder from URL

I have multiple domains which I want subfolders for using .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subfolder/index.html [L]
But this means URLs can still contain the subfolder: example.com/subfolder/img/file.png. How do I stop /subfolder from being accessible?
You need a new rule to remove /subfolder/ from you URLs like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$
RewriteCond %{THE_REQUEST} \s/+subfolder/(\S*)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$
RewriteRule ^/?$ subfolder/index.html [L]

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]

htaccess not working for www and non-www

i'm trying to redirect www.site.ru and site.ru to www.site.ru/ru_RU. But i can't make it work.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's not redirecting WWW version. Can someone tell me how to make that request. By the way, sometimes i come to situation, where in firefox it's working, but in IE it's not.
Try this code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?site\.ru$ [NC]
RewriteRule ^$ http://www.site.ru/ru_RU [L,R]
## WHAT IS THIS RULE DOING??
# RewriteCond %{REQUEST_URI} ^/news
# RewriteRule ^ /news [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
btw I had to comment out suspicious looking 2nd rule for /news. If you can explain what you're trying to do with this I can suggest you how to fix it.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ru_RU [R=permanent,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ru_RU [R=301,L]

Categories