Custom URL's to users, dynamically at a scale - url-rewriting

I need to create custom urls for my users, for ex: www.example.com/alpha, www.example.com/beta. I was creating symlinked directories, which I think worked as a good starting point for prototyping. However, now I need to do this at a scale, running on several web servers behind a load balancer. I am not sure on the right way to do this.

If you are running on an Apache server and you have access to .htaccess files then try this in a .htaccess file
RewriteEngine On
RewriteRule ^(.*)\.html$ index.php?p=$1
This will point all URL's to index.php?p=* internally.
A user will see the .html, and the server interprets it as the pointed url.
Example: yourdomain.com/cocacola.html -> yourdomain.com/index.php?p=cocacola

Related

htaccess problems ("/file/" serves file.php)

I was starting to implement mod_rewrite rules on my site when I came across some weird behaviour. I removed my htaccess file for this test, to take it out of the equation.
My local dev site is at http://dev.mydomain.com and is a virtual host.
If I go to, eg "http://dev.mydomain.com/blog/", that folder doesn't exist, but apache finds a matching php file "blog.php" and instead displays that.
This only happens when there is a matching php file - when there isn't, eg "http://dev.mydomain.com/barfblurg/" I just get a 404.
It's like there are some extra mod_rewrites going on above where the site htaccess would be - that when /file/ couldn't be resolved, it searches for other matching files and instead serves this - but there are no other htaccess files that would have an effect, so this must presumably be a config thing? I can't see anything in the apache.conf or php.ini that would cause this behaviour.
(This also doesn't happen on my live host elsewhere, so it's definitely a config thing.)
Anyone point me to where to turn that behaviour off, because it's interfering with the url rewrites I want to do?
(Apache2, OSX, 10.10.5)
This behavior is due to enabling of option MultiViews.
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To turn this off use:
Options -MultiViews
at top of your .htaccess or in Apache config/vhost file.

Redirecting from one domain to another with mod_rewrite

I've set up a site for someone that has a URL like:
www.flowers-oakville.com.
But they'd also like www.flowersoakville.com to land on the page.
The question now is, what is the best way to redirect from www.flowesoakville.com to www.flowers-oakville.com. I don't want the user's browser to show the redirection by showing the new URL in the address bar so I think Apache's Redirect command is not the solution. Plus, the Apache Redirect would take an extra trip to the browser and back that I'd like to avoid. So I think the solution needs to be a mod_rewrite, which as I understand it, does everything within the server so the round trip is avoided and the user's address bar is unchanged.
But after reading through a few mod_rewrite tutorials, it seems that mod_rewrite is mainly for redirecting files, not an entire website. Is there a simple way to do this?
Thanks for any help.
I would do this at the domain level and ensure you are using relative links or an environment variable for the hostname in the website.
Add www.flowersoakville.com as a CNAME record pointing to www.flowers-oakville.com.
This will then serve the same website, but retain the domain name as the user entered it, unless you have explicitly linked the actual domain in a link somewhere in the site.
If mode_proxy
is enabled on your server, you can use mod_rewrite (the following code) in your .htaccess file to redirect a domain to another domain without changing url in the address bar.
Try adding this to www.flowersoakville.com/.htaccess file :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?flowersoakville\.com$ [NC]
RewriteRule ^ http://www.flowers-oakville.com%{REQUEST_URI} [L,NE,P]
Note : The example above will not work if proxy module is not enabled on your server.
I wanted to pass on my experience trying to use Dwev's answer. It turns out that the DNS Manager running in my virtual server (I'm using eApps as my hosting provider) does not support "domain based redirection." That is, the left side of a CNAME record has to be in that domain. So when trying to build a CNAME record in the flowers-oakville.com record that points to flowers-oakville.com, the left side has to be xxx.flowers-oakville.com where the xxx might be something like www. I can't get the left side to be flowersoakville.com, even though flowersoakville.com is one of the domains my server is listening for.

What kind of server configuration is needed to have a laravel app working

Using cpanel I have upload into a server a laravel application. During the development I have used MAMP and it works fine.
When I upload the application into my host server, I only can see the first page:
http://myurl.com/appname/public/
When I try to navigate I always get this error:
Not Found
The requested URL /myurl.com/appname/public/account/sign-in was not found on this server.
What is the problem? Are there any special configuration for laravel applications?
Edit:
.htaccess file content:
Options -Indexes
SetEnv DEFAULT_PHP_VERSION 55
I tried to add RewriteBase /like this:
Options -Indexes
SetEnv DEFAULT_PHP_VERSION 55
RewriteBase /
But it didn't work either.
I'm using this host https://www.ecowebhosting.co.uk/?page=webhosting&tab=advanced
You didn't mention which web server you're using, but if it's apache, it looks like you need to either
Tell Apache to process .htaccess files (so it can see the rewrite rules that pass everything to index.php
Turn on the mod_rewrite module, which is the module that does the work of rewriting URLs
When you request a URL like
http://example.com/appname/public/account/sign-in
in a Laravel application, the rewrite rules turn that in
http://example.com/appname/public/index.php/account/sign-in
Behind the scenes. It looks like your server isn't setup to do this. Also, since you're serving Laravel out of a non root folder, you may need to set the RewriteBase. However, generally speaking, the ideal Laravel setup is one where the public folder is your web root. If possible, I'd configure your server so that's true as well.

How do I configure Laravel to properly work on localhost?

So I have this website, that some else build on Laravel. Since I need to make some changes I want to copy this website locally and make it work locally, so that later on I can apply the changes. I really don't want to work on the live site.
So I copied the whole website in my local folder but in order for me to see each page I have to follow this link:
http://localhost/XXXproject/html/index.php/auth/signin
I would like to know, where can I fix this. I want to remove the index.php from the url and be able to see the images without getting 500 errors everywhere. Something more like this:
http://localhost/XXXproject/html/auth/signin
And this is my htaccess.
RewriteCond %{REQUEST_URI} !^/html
RewriteRule ^(/?)(.*) /html/$2
During development I would recommend using Laravel's little HTTP server which will host the site without any external web server software such as Apache, where configuration might be an issue.
You can do this by going into the root directory of the project and typing:
php artisan serv
However if you do want to use a webserver for local development purposes with Laravel 4, you'll need to configure the rewrite rules appropriately. Although it does seem the rewriting is working to some extent, you may need to configure your webserver to consider index.php as a root document. Can't really help you more than that without more information on your webserver software and its current configuration.
Finally I solved it, I went through the installation and configuration steps from the website, and realize that I was working with a htaccess that was under wamp\www folder, I even tried making one for wamp\www\xxxproject.
But when following the steps I realized there is a htaccess inside the public folder from the project, I changed that one with the one from the website and now it works!

Multiple index files in different folders?

I just figured out that if I have multiple folders in my website such as logout and login I can then have a log out/in page within the folders called index.php
Now when someone goes to project.com/login/ it will load up the index.php file in that folder and the url will look nice.
Is there any problem in doing this?
As Candy Pointed out it is better to do this with url rewrite rather heeps of folders and index files. I went and looked it up and you can do a lot of cool things.
for the basics:
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
Instead of creating many folders which would make the site confusing (for the developer) and having numerous index.php files which would also get confusing if you were editing more than one at a time, you can use the apache mod_rewrite module which lets you transparently (the url in the address bar stays the same) redirect one URL to another. You could use this to clean up messy url's and have /login/ instead of /login/login.php or /user/1335591/ or even /user/'username'/ rather than an ugly url which is hard to remember like the following: /user/user.php?user=1335591
to start first put RewriteEngine on in your .htaccess file to turn it on
redirects are then written in the .htaccess file in sequence
Below is an example:
RewriteRule ^products/([0-9][0-9])$ /products/$1/ [R]
RewriteRule ^products/([0-9][0-9])/$ /productinfo.php?prodID=$1
If the user types in products/12 the first rule will append a trailing slash to it. The second rule will transparently redirect this URL to productinfo.php?prodID=12

Resources