.htaccess - Subdomain and path as GET var - php

Results to achieve :
http://example.com/ -> /websites/dispatcher.php
http://another-example.com/ -> /websites/dispatcher.php
http://demo.example.com/ -> /websites/dispatcher.php?subdomain=demo
http://demo.example.com/cat/page.html -> /websites/dispatcher.php?subdomain=demo&path=/cat/page.html
With the .htaccess i have, it's working except for the "demo.exemple.com" who return a 502 error.
# Parse the subdomain as a variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?subdomain=%1
# Map all requests to the 'path' get variable in distpacher.php
RewriteRule ^distpacher\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /distpacher.php?path=$1 [L,QSA]
I'm having a hard time... any help, please?

Replace your 2 rules by these 2 rules:
# Map all requests to the 'path' get variable in dispatcher.php
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^/?$ /dispatcher.php?subdomain=%1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^(.+)$ /dispatcher.php?subdomain=%1&path=$1 [L,QSA]

Related

htaccess multi language not working

I use multiple languages. But, I have a problem. This my code not working.
The default language always works.
For example :
www.mysite.com/tr/hakkimizda
www.mysite.com/en/about-us
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^dil=([a-z]{2})(?:&|$) [NC]
RewriteRule !^[a-z]{2}/ /%1/%{REQUEST_URI}? [L,NC,R=301]
# default
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^dil=.+(&|$) [NC]
RewriteRule !^tr/ /tr/%{REQUEST_URI} [L,NC,R=301]
**language.php**
$dil=$_GET["dil"];
if(file_exists("dil/".$dil.".php")){
$_SESSION["dil"]=$dil;
header("Location:/");
}else{
header("Location:tr");
}
Link type
language.php?dil=tr (location index.php)
language.php?dil=en (location index.php)
I want to be
www.mysite.com/tr/hakkimizda
www.mysite.com/en/about-us
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^dil=([a-z]{2})(?:&|$) [NC]
RewriteRule !^[a-z]{2}/ /%1/%{REQUEST_URI}? [L,NC,R=301]
# missing rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/ language.php?dil=$1 [L,NC]
# default
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^dil=.+(&|$) [NC]
RewriteRule !^tr/ /tr/%{REQUEST_URI} [L,NC,R=301]

Getting value with htaccess and pagination for root

This url
example.com/videos/load.php?cat=video-category
would become
example.com/videos/video-category
Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&p=2&limit=20
it would become
example.com/videos/video-category?p=2&limit=20
(this links also work)
example.com/videos/video-category?p=2
example.com/videos/video-category?limit=20
Also, there is another root url
example.com/videos/load.php?cat=video-category&name=video-name
it would become
example.com/videos/video-category/video-name
Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&name=video-name&p=2&limit=20
And it would become
example.com/videos/video-category/video-name?p=2&limit=20
(this links also work)
example.com/videos/video-category/video-name?p=2
example.com/videos/video-category/video-name?limit=20
Here is my htaccess (into videos folder)
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \ /+videos/load\.php\?cat=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ %1?%2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]
how to do this.. plz help me.. thanks..
Have it this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&name=([^&\s]+)&?(\S*)
RewriteRule ^ %1/%2?%3 [L,R=302]
RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&?(\S*)
RewriteRule ^ %1?%2 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ load.php?cat=$1&name=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [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]

Htaccess redirect allways to index except if specific subdirectory

So I have this task, where I have to make basically two webpages under one domain. Main page always have to go through index.php. Except if / subdirectory (under subdirectory there is another webpage) - then works with subdirectory
Example:
example.com -> goes to index.php
example.com/subdirectory ->goes to /subdirectory/index.php
example.com/subdirectory/peter -> goes to /subdirectory/peter/
example.com/example2 -> goes to index.php
This is what I have managed so far:
#If
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !/subdirectory/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite
RewriteRule ^(.*)$ /forums/$1 [L]
# IF
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite
RewriteRule ^(/)?$ index.php [L]
Honestly I am out of ideas...
Found out how to do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/forums
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
atleast for now its workin.

Configure mod_rewrite to add "www" and to support MVC

I would like to add www into the URL if the users forgot to type and still supporting MVC by redirecting to the index.php with [QSA]
This is my current working .htaccess
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]
Using this .htaccess everything works fine, I can type www.abc.com/xxx/yyy to visit any specific pages
Anyway because I'm using Facebook authentication so I must prevent my users from typing the site URL without www, so I tried to edit my .htaccess
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteRule ^(.*)$ http://wwww.abc.com/$1 [R=301,QSA]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]
With this configuration if I typed abc.com I was redirected to www.abc.com, but if I typed abc.com/xxx/yyy I was only redirected as www.abc.com instead of being redirected to the specific page.
I also tried
# 1
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteRule ^(.*)$ http://wwww.abc.com/$1 [QSA]
# 2
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteRule ^(.*)$ http://wwww.abc.com/index.php [QSA]
# 3
RewriteCond %{HTTP_HOST} ^abc\.com$
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://wwww.abc.com/$1 [L,QSA]
None of them is working, please guide what to do
Thanks.
Add this 2 lines above your .htaccess file
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
so your final htaccess will look like this
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

Categories