htaccess rewrite sub directory - php

I have a production copy and a test copy of my website on bluehost. Each website is in it's own directory in public_html folder, one named prod another named test. According to bluehost knoweldge base here: https://my.bluehost.com/cgi/help/347 you setup an htaccess file to rewrite requests coming into the public_html folder to my prod folder. This works pretty well thus far. Something I noticed recently though was with these rewrite settings that if you attempt to load a website file from another directory inside the main website folder e.g. /prod/testfolder without a forward slash on the end it will redirect you to www.mysite.com/prod/testfolder/ instead of staying on www.mysite.com/testfolder. This condition does not happen if you specify the extra forward slash like so.... www.mysite.com/testfolder/
Here is my rewrite rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/prod/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /prod/$1
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteRule ^(/)?$ prod/index.php [L]
I'm not an expert when it comes to rewriting urls using htaccess however I suspect the first chunk of rewrite rules is the cause of this. BTW an example of why this makes a difference is that I setup a blog on this site and if you attempt to visit the blog at www.mysite.com/blog it redirects to www.mysite.com/prod/blog/ which defeats the purpose of using htaccess to mask the prod folder in the first place. Can anyone tell me how I should go about fixing this and maybe explain why it's happening? Thank you!

Related

htaccess for domain and subdomain with laravel

I'm trying to figure out how to set up a domain and subdomain to work on a shared hosting account. It is a Laravel 5.1 application.
My access file is
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
I just purchased another domain and added it on, but I get a 500 error. I renamed the access file and then it worked. So it has something to do with the access file. Essentially I want two separate domains with and I'm wanting two separate laravel applications, one for each.
I'm not familiar with atacceess.
Maybe, you get a redirect loop, because the rule isn't protected by a condition. Although the default htaccess of Laravel should already contain them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Olaf Dietsche's answer does work for me.
Here is another thing I came upon a website that also worked just before I saw his post. I guess I was reading that this would send a 404 to that directory.
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?main-topdomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain-folder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
So along with this comes another question if anyone is in my boat. I have my subdirectory inside of my root directory.***
dlaugh.com/public_html
laravel folders and access**
inluding
app
bootstrap
config
database
etc...
but also I have my sub folder
app
bootstrap
config
database
etc...
**mysubdomain in that folder**
Is it better practice to put
-main_domain_folder and
-subdomain_folder
in public_html
and then the
/app
/config
/database
would be in the main_domain_folder rather than passing the subdomain through the main domain?

Create a Subdomain in godaddy hosting using PHP

Is there any easiest way to create a sub domain using PHP code on godaddy.com hosting?
e.g:
jon.mywebsite.com
jessie.mywebsite.com
etc
you need to create a A record to serve all your subdomains
*.your-site.com IN A YOUR-IP-ADDRESS
then you need to create a .htaccess file and
RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}
this will ignore images (SEO friendly URLs).
Now you can redirect your users to $userName.your-site.com
Alternatively try this:
setup your application so your users goes to
your-site.com/user
your .htaccess should look like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$ index.php?username=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*) index.php?username=%1
so now when you hit the index.php it will grab the user and redirect to $user.your-site.com as a custom subdomain. (in this case usernames are limited to a-z characters)
I dont think with PHP Code you can but with the help of .htaccess i think it is possible.
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+jon/
RewriteRule ^jon/(.*)$ http://jon.mywebsite.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+jessie/
RewriteRule ^jessie/(.*)$ http://jessie.mywebsite.com/$1 [L,R=301]
RewriteRule ^(jon|jessie)/ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jon\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jon/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jessie\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jessie/$1 [L]
You can make subdomain much easier using control panel, why to do it the hard way ?
This is an old question, but in case anyone is looking for the answer. This is specific to GoDaddy, but other Hosts may do something similar to this.
After you setup your DNS A record you need to go to Hosted Domains and add the subdomain folder name under the root domain account.
Go to Hosted Domains.
Under Hosted Domains you will see a root domain table column.
You will also see a Subdomains table column with an "Add" link.
This link is for adding subdomains to a root domain.
Type in/enter the name of the subdomain folder.
Create either an index.php or index.html file and upload it to your subdomain folder and echo something like "Setup Completed". I noticed that if you are doing something like creating a restricted "api" subdomain for example and the GD script cannot access the index file in your subdomain folder then the "process" does not complete until you add a plain index.php or index.html file in your subdomain folder. After that is done then you can add anything you want in that subdomain folder/site.

All files are parsed as PHP after rewriting

I have a website located at example.com.
I've built a Laravel-based CMS for managing small sites and now try to set up rewriting.
In the root directory my .htaccess has:
RewriteRule ^(lorem|ipsum)/?(.*) cms/public/$2 [L,PT]
because I want to rewrite example.com/lorem and example.com/ipsum to my CMS. This works fine, but then I have another .htaccess in cms/public/:
RewriteRule assets/(.*) assets/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php
That's because I want to have cms/public/assets/ available from example.com/lorem/any/location/assets/file.css and redirect everything else to index.php.
Seems to work, however I've noticed that all files, including images, are served as text/html and PHP code is executed even in non-PHP files. Why is that happening and how to fix it?
In fact, rewriting was not to blame. I put Use php54 into cms/.htaccess and this line should've been preceded by :Location *.php. After adding it everything works perfectly.

Redirect rules for 404 content using .htaccess

It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.
Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.
Thanks for any help.
You could do something like
Edited based on comments:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule dev/(.*)$ $1 [R=301,L]
Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.
This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example

mod_rewrite redirect from any page to home page

I need to redirect if someone goes to any folder e.g. http://site.com/images/ or http://site.com/images to http://site.com.
Unless he goes to file e.g. http://site.com/images/index.php in this case it does not redirect
now i use
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
but i think its not perfect because e.g. if someone comes on http://www.site.com it does not work.
Keep in mind due to server configuration i need to put .htaccess in every folder.
I believe that what you are looking for is the -f flag:
RewriteCond %{REQUEST_FILENAME} !-f
See http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you are only looking at stopping someone from getting a list of the files you have in the folder, you may want to consider instead adding:
Options -Indexes
This will tell apache not to display a directory list if no index page is present in the given directory.
See http://httpd.apache.org/docs/2.2/mod/core.html#options
The last thing to note, if you are not familiar with how .htaccess works, apache scans the current folder and any of its parent folders - you should be okay with only adding a single .htaccess file in your web root for http://site.com. This has been covered in another question here.

Categories