How to rewrite a subdomain with .htaccess? - php

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]

Related

how to redirect old url with parameters to new seo url using htaccess

want to redirect param urls to seo friendly urls
https://example.com/a.php?slug=vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present
to
https://example.com/vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present
and
https://example.com/tag.php?id=Virtual-reality
to
https://example.com/tag/Virtual-reality
my current .htaccess code
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ a.php?slug=$1 [L]
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# external redirect to redirect old URL to pretty URL
RewriteCond %{THE_REQUEST} /a\.php\?slug=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{THE_REQUEST} /tag\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /tag/%1? [R=301,L,NE]
RewriteRule ^tag/([\w-]+)/?$ tag.php?id=$1 [L,QSA,NC]
# internal rewrite to forward to actual URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ a.php?slug=$1 [L,QSA]

How can I do the following in .htaccess?

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]

Multiple RewriteRules for single RewriteCond

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]

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]

Wildcard subdomain mod-rewrite does not work

I have a wildcard subdomain *.domain.com assigned to public_html/.
I want to do like this:
For example, /folder1/index.php is based on state name(?state=statename).
For the /folder1/folder2/index.php, it will be based on unique name(?name=uniquename).
So, www.domain.com/folder1/index.php?state=statename will be statename.domain.com
and www.domain.com/folder1/folder2/index.php?name=uniquename will be uniquename.domain.com
This is my code
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
The problem is it redirect back to public_html directory. Is there any problem with the code?
Old Code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^$ /index [L]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^(index)/?$ /$1.php?name=%2 [L,NC,QSA]
Code explanation : Whenever user enter uniquename.domain.com, it will automatically go to www.domain.com/index.php?name=uniquename and the uniquename.domain.com in url bar would not change.
The differences for the new problem is there are different state directory and the domain would be state1.uniquename.domain.com. The 'state1.uniquename.domain.com' in the url bar should not change too.
Based on your comments. Make sure DOCUMENT_ROOT for www.domain.com, state1.domain.com, state2.domain.com is public_html
Try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /state/%1/client/index.php?name=%2&page=%{REQUEST_URI} [L,NC]

Categories