URL rewriting with subdomain structure in php - php

I am trying to create a URL like category.\ArticleName
This is what I have written in my .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).msite.com/([a-zA-Z]+) [NC]
RewriteRule (.*) $1/Html/HomePage.php?Category=%1&name=%2 [NC,QSA]
I actaully tried
RewriteCond %{HTTP_HOST} ^(.*).msite.com$ [NC]
RewriteRule (.*) $1/Html/HomePage.php?Category=%1 [NC,QSA]
It worked fine means wild-card subdomain etc are enabled.

Close, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*).msite.com$ [NC]
RewriteRule (.*) /Html/HomePage.php?Category=%1&name=$1 [L,NC,QSA]
You'll need the two extra conditions in the beginning so that valid requests don't get routed to HomePage.php.

Related

Htaccess for external domain

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
This is my current code
subdomain.domain.com/querystring
The condition is domain.com
Is there a way I can achieve the same thing
but the domain.com can be dynamic, means it could be 123456.co or qwerty1234.xyz/querystring
Yet it able capture
Domain: qwerty1234.xyz
Query String: querystring
How do I make my htaccess to check if domain is not containing domain.com, it will use load.php
but pass the domain & its query string to load.php as ?domain=$domain&query=$querystring
Thanks!
Updated .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
RewriteCond %{REQUEST_URI} !(\.)?(css|js|png|jpg|gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
You should be able to add this to your rules. You don't have to do anything special to capture the querystring. Simply keep the QSA flag and any additional query string on the request will also be sent to load.php.
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.)?(css|js|png|jpg|gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ load.php?domain=%1 [NC,QSA,L]

Cannot access subpage url of subdomain

I did a rewrite rule for wildcard subdomain access to allow me to access any countries of my list page as follows:
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.net$
RewriteCond %{HTTP_HOST} !^www\.mydomain\.net$
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|bmp)$ [NC]
RewriteRule (.*) pages/list.php?country=%1 [L]
This is working fine.
Now. I do a rewrite rule to access another page on the domain but i keep getting referred back to the list page.
RewriteRule ^ad/([A-Za-z0-9-]+)?$ pages/details.php?alias=$1 [NC,L]
My end result is to access eg: http://us.mydomain.net/ad/alias but it keeps referring me back to the list page.
UPDATED REWRITE RULES(AS OF 14 MAY)
RewriteEngine on
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
RewriteRule ^cat/([A-Za-z0-9-]+)/?$ ?category=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(.+)\.ibizportal\.net$
RewriteCond %{HTTP_HOST} !^www\.ibizportal\.net$
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|bmp)$ [NC]
RewriteRule (.*) pages/list.php?country=%1 [L]
Change first rule to:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^cat/([A-Za-z0-9-]+)/?$ ?category=$1 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^((?!www).+)\.mydomain\.net$ [NC]
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteRule (.+) pages/list.php?country=%1 [L,QSA]

Sub domain pretty URL in PHP

I created a sub domain for my application (http://articles.myloanguroo.com/inside.php/?short=1). Now I want to create a pretty URL for this like (http://articles.myloanguroo.com/inside/1)
My .htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ inside.php?short=$1
RewriteCond %{HTTP_HOST} !^www\.myloanguroo\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.myloanguroo\.com$ [NC]
This is not working for me and is showing a 404 error.
How can I create subdomain pretty URLs in PHP?
Your regex should start with ^inside/ and keep conditions before RewriteRule:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.myloanguroo\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^inside/(\w+)/?$ inside.php?short=$1 [L,QSA]

htaccess remove www and redirect to accessed url

I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks

.htaccess Only for www. Subdomain

How do I construct my .htaccess file so that it adds www to the beginning of the address mysite.com, but doesn't add www. to the beginning of other subdomains such as test.mysite.com? My .htaccess file currently looks like this:
Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
Something like:
RewriteCond %{HTTP_HOST} ^mysite.com
Instead of
RewriteCond %{HTTP_HOST} !^www\.
I just don't know the darn rule to get only the characters for domain naming so i'll try this but it might fail on some subjects:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-]+(\.[a-z]{2,4})+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
This will force all domains that START with one word and a TLD to be forwarded to www.samedomain.tld but subdomains wont be hit...

Categories