htaccess RewriteMode + Hide the original directory - .htaccess

So i have installed Laravel and also using htaccess rewrite so i can change the directory for the main domain. It's not the /public_html - its /public_html/examplecom/public
When i open the website: example.com (i have Laravel plugin which redirects me to example.com/en, and the website is fully visible.
The problem is when i try to open: example.com/examplecom/public ... it doesn't hide and return to example.com. It's redirecting to example.com/examplecom/public/en (the Laravel redirect).
So the htaccess code i'm using is this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/examplecom/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /examplecom/public/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ examplecom/public/index.php [L]
Can someone help? Thank you :)

Forget it... i made it with PHP...
For those who have the same problem:
$page = $_SERVER['REQUEST_URI'];
if (preg_match('|/examplecom/public/(.*)|i', $page)) {
preg_match('|/examplecom/public/(.*)|i', $page, $exit);
exit(header('Location: /'.$exit[1]));
}

Related

Force https and non-www urls, not working in subdomain

In the public_html folder which is the root folder of my main site thesite.com, I have this htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^m\.thesite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^blog\.thesite\.com$ [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.thesite\.com [NC]
RewriteRule ^(.*)$ https://thesite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2&did=$3 [L]
RewriteRule ^category/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2 [L]
RewriteRule ^category/([^/\.]+)/?$ category\_product\_list.php?mid=$1 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3&mid=$4 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/?$ product.php?id=$2 [L]
RewriteRule ^offer/(.*) product_offer.php?ind=$1
RewriteRule ^([^/\.]+)/?([^/\.]*)/?$ $1.php [QSA,L]
Here, am forcing non-www url as well as forcing https URL for the thesite.com. And I have two subdomains: m.thesite.com and blog.thesite.com.
Both are in separate subfolders inside public_html.
The m.thesite.com is in /public_html/m.thesite.com/ folder. And it has this htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.m\.thesite\.com [NC]
RewriteRule ^(.*)$ https://m.zapteka.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2&did=$3 [L]
RewriteRule ^category/([^/\.]+)/([^/\.]+)/?$ category\_product\_list.php?mid=$1&sid=$2 [L]
RewriteRule ^category/([^/\.]+)/?$ category\_product\_list.php?mid=$1 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3&mid=$4 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/([0-9]+)/?$ product.php?id=$2&vid=$3 [L]
RewriteRule ^p/([^/\.]+)/([^/\.]+)/([0-9]+)/?$ product.php?id=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]*)/?$ $1.php [QSA,L]
RewriteRule ^offer/(.*) product_offer.php?ind=$1
For this subdomain also, I need to force https urls as well as non-www urls.
It seems to be working for the main site. I mean thesite.com. But when I try to access the m.thesite.com, it seems to be internally redirecting to the files of thesite.com instead of the m.thesite.com. I mean the url is being shown as https://m.thesite.com/ but the pages are from https://thesite.com
I have been trying to debug the issue for several hours now. Probably it could be something small that I missed. Any ideas?
BTW, thesite.com is having Wildcard SSL installed.
EDIT
Am sorry if my explanation was confusing. When I said url is showing as https://m.thesite.com/ but the content is from https://thesite.com/, I meant to say that in browser the url is shown as https://m.thesite.com. But the template file contents where from the desktop version of the main site thesite.com. The m.thesite.com is mobile version of the site. The pages that displays the contents where separate in both the desktop and mobile version of the site.
Like you see in the htaccess code that I included above, there is some PHP files inside the /public_html/ folder for the desktop version. All the files for the mobile version of the site is in /public_html/m.thesite.com/ folder.
So, in short, when I try to access the https://m.thesite.com/, it is displaying the index.php page from /public_html/index.php instead of /public_html/m.thesite.com/
So that's how I found that there's issue in the routing.
EDIT
VirtualHost entries fetched by using this command /usr/local/apache/bin/httpd -S:
I have also checked the Apache version and saw that it's 2.2.27 using the command: httpd -V
So I tried adding this line in the htaccess file:
RewriteLog "/logs/rewrite.log"
RewriteLogLevel 9
But it was giving internal server error. So I tried this line instead also:
LogLevel mod_rewrite.c:trace8
That was also giving me internal server error when site is accessed. So I commented it out.

redirect non-www and https to subfolder of the root

I am trying to make test env for this website. I have the same website on live and it is working.
I am trying to redirect the request to do the redirect to this usbfolder(OPS).
This is what I wrote so far but it sounds it is redirecting to root.
If I remove the https it works but with https it doesn't. It sounds it's redirected to root
RewriteEngine on
RewriteBase /OPS/
RewriteCond %{HTTP_HOST} ^dev.domain\.co\.uk$
RewriteCond %{REQUEST_URI} !^/OPS/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /OPS/$1 [L]
RewriteCond %{HTTP_HOST} ^dev.domain\.co\.uk$
RewriteRule ^(/)?$ /OPS/index.php [L]
Thank you all for your concern. As #anubhava mentioned the problem was with my document root. We revert back the htaccess to what ever it was. and overwrite the DOCUMENT_ROOT in top of the page and it is working:
if (strpos($_SERVER['DOCUMENT_ROOT'], '/var/www/websites/dev') !== false) {
// OPS(sub-folder)
$_SERVER['DOCUMENT_ROOT'] .= '/OPS';
}
I would like to over-write document root in htaccess if it would have been possible. any way this is the answer I came up with so far.

Syntax with .htaccess URL rewrite

I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/

Hide folders in url, rewrite url

I have a project with a special hosting structure:
www
domains
projectName (example.com)
I have to set the baseUrl config.baseURL = http://example.com/domains/example.com/.
Url example http://example.com/domains/example.com/index.php?id=4.
If I use only config.baseURL = http://example.com/. The site works but sessions do not work and user cannot log (he can log in but after reload he is logged out)
I need to have url only with http://example.com/index.php?id=x.
There is 1 htaccess in the www folder with these rules.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]
And another is in my project folder.
I am sure there will be just an easy htaccess rule. But I do not have any experience with it. I tried to use the solution from Mod Rewrite Hide Folder but it did not help. I tried to use it in both htaccess files.
Any ideas?
Thank you!
You could try setting the TypoScript option config.absRefPrefix to /domain/www.example.com/
See http://buzz.typo3.org/people/soeren-malling/article/baseurl-is-dead-long-live-absrefprefix/ and http://wiki.typo3.org/TSref/CONFIG

Htaccess redirect with cakePHP

I have a cakePHP app working at http://domain1.com/domain2, and want to point http://domain2.com/ to this application.
I have done this change the document root of domain2 to public_html/domain2, but, when I go to: domain2.com all css, javascript and images are not loaded, with a message saying controller not found.
What I can do?
All domains have a folder with their domains without the TLD at domain1.com, I think if I create a htaccess and get the domain without TLD, and finally change rewriteBase, will work as expected, but don't know how to do this.
Current htaccess of domain2:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
You can try adding an htaccess file in domain2's document root with the following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(css|js|png|jpe?g|gif|bmp|ico)$ [NC]
RewriteRule ^/?domain2/(.*) /$1 [L,PT]
This should remove the domain2 part of the links.

Resources