Pointing a Sub-domain to a sub directory - php

Using Laravel I'm generating php pages in to a sub-directory called webpage. I want to allow users to access pages using sub-domain. For example, I've generated a file whoami.php into webpage directory. If people try to access whoami.domain.com, he can see the whoami.php. I've crated an index.php inside webpage which can extract alias and show the file. The think I need is to pass the subdomain to index.php file without redirecting. I wrote htaccess as follow
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
#sub domain custom part
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/webpage/%1/$1 [L,NC,QSA]
</IfModule>
But I'm redirecting into http://domain.com/webpage/whoami.
I tried to make my requirements clearly and pasted what I tried. Can you please help me to figure out a way?
EDIT 1
Based on #Starkeen's answer I tried the following code an now in a redirect loop (http://whoami.domain.com/webpage/whoami/webpage/whoami/......).
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
#sub domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ /webpage/%1/$1 [L,NC,R]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

To redirect subdomain to a subfolder, You probably need to put the following right after the "RewriteEngine On" line :
RewriteCond %{HTTP_HOST} ^(sub)\.domain\.com$
RewriteRule ^(.*)$ /webpage/%1/$1 [NC,R,L]

I got a every simple solution. As I'm using Laravel and that already use an index.php as bootstrap, I wrote some PHP codes there to achieve my goal. You guys can use if needed. And please advice me if my solution isn't good enough. Thanks
My Solution
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link = str_replace(array('http://', 'https://', 'www.'), '', $link);
$splitted = explode(".", $link);
if($splitted[0] != 'domain'){
$file_name = $splitted[0].'.php';
$file_loc = 'webpage/'.$file_name;
if(file_exists($file_loc)) {
include($file_loc);
} else {
header('Location: http://domain.com/not-found');
}
die();
}

Related

Removing 2 folders from URL and redirecting to page

I am creating a new web app and this is how my project folders look like:
So as you can see I have renamed that htaccess in the root folder because I am not using that one and I am using the one situated inside of the public folder. I am trying to change my URLs from this:
http://example.site/public/pages/about
to:
http://example.site/about
I was able to remove the .php from the file extensions but the code I added to remove the folders from the URL is not working.
Note: in the root folder (App/index.php) the index.php file simply has this:
<?php
header('Location: public/pages');
so I am just using this to send to the correct folder.
This is what I have so far:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /App/public/
RewriteRule ^pages/(.+)$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]
I have seen this question/answer from this url:
Remove / Rewrite 2 directory names from URL
but it is not working for me. Looking forward to anyone who can help, thanks.
Assuming ``htdocs/app/is yourDocumentRootforapp.blu` domain.
Inside htdocs/app/public/.htaccess you may use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if URL has /pages/ then remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pages/(.+)$ /$1 [L,R=301,NC]
# landing page for app subdomain to show index.php
RewriteRule ^$ pages/index.php [L]
# check & rewrite if a matching .php file exists in pages/ subdirectory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/pages/$1.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]
Besides this you will need to use this code in htdocs/app/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# send all the traffic to /public/
RewriteRule .* public/$0 [L]

Redirect all subdomains from HTTP to HTTPS using htaccess

I have domain and its working with https for example
http://example.com/
Now I am creating sub domains like demo1.example.com, demo2.example.com and so on.
I need all sub domains must be redirect and access by only https.
I have write the code in my .htaccess see below but its always redirecting to the main domain when I access my sub domains.
My htaccess code is
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
EDIT
I have main website at root and all other sub-domains code at one of folder. So whenever any sub-domain created that will point to that folder only.
The following worked for me. NOTE: if your subdomain has its own .htaccess file (for example a WordPress site in your subdomain), you will have to edit that .htaccess file in addition to the main, domain .htaccess.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domian\.com$
RewriteRule ^https://%1.domain.com%{REQUEST_URI} [NE,L,R]
You can add other rewrite conditions above or below this, depending on when you want them executed. For example, Reghbendra Nayak's "Handle Front Controller..." code can be placed blow the https 'force' and it will execute.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]

How to stop redirection to index.php main file in code igniter?

I have a website domain.com which is based in code igniter. When someone enters a wrong url it redirects it to https://domain.com/index.php
I want to redirect redirect it https://domain.com and do not want index.php to be present in the urls. I have tried everything in htaccess file but it dooesnt work.
Here is my htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
AddHandler application/x-httpd-php55 .php55 .php
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
If I understand rightly, you want to redirect index.php to the root?
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Let me know if not

Remove index.php from url Laravel 5

URLs are working fine in my application. I mean they are pretty URLs. Like http://www.example.com/
But it also works when you access the page with index.php like http://www.example.com/index.php, which I don't want because it is showing two links in sitemap for one page. One page without index.php and another with index.php. Demonstration of the sitemap is here https://www.xml-sitemaps.com/details-eln.6762418.html
Here is the .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php\sHTTP.*$
RewriteRule ^ /%1 [R=301,L]

How can I change the URL in the browser by htaccess?

I want to change my url by htaccess from
http://example.com/offer?search=something to http://example.com/offer/something
The following htaccess works from http://example.com/offer/something but if I typing the http://example.com/offer?search=something then the url is not changing to http://example.com/offer/something.
RewriteEngine On
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]
Should I use "rewritecond"? What is the correct htaccess in this case?
I try make it in Laravel framwork. I have only one htaccess and this is the content, currently:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{QUERY_STRING} ^search=([-a-zA-Z0-9]+)
RewriteRule ^offer$ /offer/%1? [R=301,L]
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]
</IfModule>
UPDATE: Full .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect URL with GET parameter to pretty URL
RewriteCond %{THE_REQUEST} \s/+offer\?search=([^\s&]+) [NC]
RewriteRule ^ /offer/%1? [R=301,L]
# rewrite from pretty URL to actual handle
RewriteRule ^(offer/[^/]+)/?$ index.php/$1 [L,QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
How it prevents looping: THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite rules, unline %{REQUEST_URI} which gets overwritten after other rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1
You also need to include a redirect from the url that contain a query string. I haven't tested this, but the first rule should catch the first URL and redirect it to the new format. The second rule should catch the new format and pass the correct values to the search parameter.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=([-a-zA-Z0-9]+)
RewriteRule ^offer$ /offer/%1? [R=301,L]
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]

Categories