I'm looking into the feasibility of using PHP - instead of mod_rewrite - to handle URL canonicalization. I'm looking to be able to map a large number of different URLs to a given physical PHP page, and handle 301's and 404's in a more centralized and maintainable way. This will include common misspellings, aliases, search engine friendly URL parameters, and the like. These needs seem well outside the power of mod_rewrite, so I'm looking into other options.
I'm thinking I would create a canonical.php script which I map every page to with the following in .htaccess (borrowed from this post):
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ canonical.php/$1?%{QUERY_STRING} [L]
And then canonical.php would do whatever URL parsing / db lookups / redirects / etc. are necessary, then simply include /the/appropriate/file.php for the given request.
Is this a reasonable course of action? Is such functionality actually feasible with mod_rewrite directly? (DB lookups and the like aside) will this be distinctly slower than mod_rewrite? Is there any other methodology that's more robust than a PHP wrapper?
You're talking about routing, which plenty of frameworks do. Take a look at this answer: https://stackoverflow.com/questions/115629/simplest-php-routing-framework
What I would suggest and what I use is a php file that gets called everytime when a 404 is encountered and it stores the url encountered.
Then once a week I go to the management console and I map the wrongly spelled mistyped, old urls, searchengine's history url's to the current existing urls then I hit the parse button and it spews out a new updated .htaccess file out for me.
That way you lighten the load on your database, loading time, compiling time and redirecting time.
Just my 2 cents.
Haven't had a chance to test this, but this answer pointed me at Apache's FallbackResource directive, which sounds very promising.
Related
I usually use this site to solve my problems, but for the first time I couldn't find a proper question, so forgive me if it actually exists!
Currently, I have valid URLs in such format:
http://www.example.com/index/modules/news/article.php?storyid=15807
That the number at the end is generated dynamically by CMS and therefore changes for any new content published.
In order to use shorter URLs, I want these pages to be accessible by subdomain in such format:
http://news.example.com/n15807
Please help me and let me know if there is a better option rather than using htaccess.
Sure! You can use mod_rewrite in apache's virtual host config file.
Sarcasm aside, the way to reroute a url is to use the tool that accepts the url and does something with it, which in the case of the typical php installation is apache. If PHP were able to accept connections without the use of an http server, then PHP could do it. I suppose one could build a server out of php and run it as a singleton, but I've never heard of such a thing.
So here's the thing, using .htaccess and mod_rewrite isn't so hard, if you understand what you're trying to do. In your case, you want to be able to translate news.example.com/n15807 into that really long uri. That really long uri is what the server will actually load.
mod_rewrite in effect matches against a regular expression and replaces it with another uri. So you would attempt to match something like ^([^w]{3}).example.com/([a-z])([0-9]+)$ and use it to replace values in /index/modules/$1/article.php?storyid=$3 (I have no idea what the n in front of the number is supposed to mean.)
The regex I gave is intended only to point you in the right direction; I haven't tested it. [EDIT - It definitely won't work as written; it's been a while since I worked on mod_rewrite. But the idea is still valid. FWIW, I wouldn't try to use subdomains, though]
Summary: Use the tool that works with the urls. The most accessible way is with .htaccess. Once you figure it out, you're done. You can reuse it over and over in future websites.
After hours of searches I finally got it!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^news.example.com$
RewriteRule ^news/(.*)$ http://www.example.com/index/modules/news/article.php?storyid=$1\#siteBody [R=301,L,NE]
Thought it might help someone someday not to spend as much time as I did!
I am developing a site in core php and i have link such as
Read More
So my question is to remove the .php extension from all the site and also from the links and url must be nice like http://www.example.com/news/ not like http://www.example.com/news.php?news_id = 4
So please if any one has idea how to do that please tell me
I will be summarizing my comments so you can mark this post here as solution for the next person having your problem. This makes finding the answer easier somehow
You need a webserver which supports rewriting of URLs. Most people perhaps use the apache2 webserver which does supports this. The module for apache doing this is called "mod_rewrite". You probably (depending on your configuration) need to enable it first.
Clean and beautiful URLs are called "smart-urls", for this term makes searching for tutorials, guides and answers much easier.
To make the mod_rewrite work and rewrite your URLs you need to enable the module for your current project/directory and write some rules down.
You can do this within your .htaccess file and for your example it will look something like this:
.htaccess:
RewriteEngine On
RewriteRule ^news/([0-9]+)\.?.*$ news.php?id=$1 [NC,L]
This will cause the server internally (and without the user can see) to rewrite something like this:
http://www.foo.de/news/1337.my-awesome-first-newspost
to
http://www.foo.de/news.php?id=1337
Remark: Everything behind the News ID is ignored
As you can see rules are written as regular expressions which enables you to create really flexible rules.
You can also write multiple rules which depend on each other or just become applied one after the other.
I found a page enabling you to test your rules because this is always a bit hard "debugging" when doing it on your server machine:
http://htaccess.madewithlove.be/
for some days now I have been trying to make a simple mod_rewrite rule to create friendly URLs, my web host have mod_rewrite enabled but I just can't get it to work.
All the next questions where posted by me:
.htacces to create friendly URLs
Friendly URLs with .htaccess
.htacces NOT working…
None of the answers worked, so I'm thinking now using simple php routing instead and I wanted to know if there is a big performance or SEO difference between the two. And if there is, maybe you know how to fix the mod_rewrite problems posted in my questions.
Thanks.
If you're using PHP routing for PHP files only, it would be no problem performance-wise: The interpreter will get started anyway, a new process started, memory allocated etc.
But if you are planning to route requests for static resources like images and style sheets as well, however, don't use PHP routing under any circumstance. It's way too resource-intensive and not what PHP was built for.
I'd say mod_rewrite is the better, leaner solution and it's worth trying to figure it out.
I prefer routing that kicks in when the requested file doesn't exist, like this in Lighttpd:
server.error-handler-404 = "/index.php"
Provided you find out how to do this in Apache, your script would be more cross webserver compatible, since Apache's mod_rewrite conditions in .htaccess won't work on Lighttpd.
I am working on a web portal (PHP/MYSQL) which have 3 sections photos, videos, articles. Each section has its own submission form using which user will submit photos, videos or articles corresponding to their accounts.
So, that URLS which I thought should be like
http://example.com/
http://example.com/about/
http://example.com/contact/
http://example.com/help/
http://example.com/photos/
http://example.com/photos/browse/
http://example.com/photos/submit/
http://example.com/photos/edit/
etc.
Same as for videos and articles sections. Now I am confused here to what to use.
Should I use .htaccess to create such type of URLs?
Or I should use folders with index.php in it for /photos/ or /photos/browse/ like this. All folders will have index.php so they will be accessed by folder names and URL will look like this http://example.com/photos/browse/
Or I should go traditionally and use photos.php and browse-photos.php . But this method not looks good and organized.
I can achieve this by htaccess easily but some peoples says that it will slow down your site, because it will process htaccess every time a URL will be requested.
I have one more question, how the sites like digg, flickr does this URL formation. These are pretty big site with millions of page-views a day, so are they not using htaccess?
Please guide me!
Thanks.
You can use .htaccess and apache's mod_rewrite (or similar for other web servers) for this. With mod_rewrite you'll be rewriting incoming urls to fit your script schema. The slowdown is negligible. Most big sites use this kind of trick.
So for a rough example, lets say you have the following working application:
./index.php
./index.php?section=about
./index.php?section=contact
./index.php?section=help
./photos.php
./photos.php?section=browse
./photos.php?section=submit
./photos.php?section=edit
Then you can put this in .htaccess
# Turn on URL rewriting
RewriteEngine On
# These are very verbose to show the point. Later you can make them smarter
RewriteRule about index.php?section=about
RewriteRule contact index.php?section=contact
#all under photos/* goes to photos.php
RewriteRule photos/(.*) photos.php?section=$1
And then place your code in the right places in index.php and photos.php
I can achieve this by htaccess easily but some peoples says that it will slow down your site, because it will process htaccess every time a URL will be requested.
For very, very, very high-traffic sites it is indeed important to see that there are not too many regex-based rewrite rules because they are somewhat performance intensive.
Most of us don't need to worry, though. Go with Apache.
.htaccess most likely won't be your bottleneck, if you need really high performance you won't get around measuring yourself where the problems are. Really big sites are not a good reference, at that scale the problems are very different.
The most flexible and common way is to use mod_rewrite to redirect to one page that then calls the appropriate part of your application.
I would not worry about performance at this point, but more about flexibility and maintainability.
You can also put your rewrite rule in the apache main config and save the overhead of .htaccess, that should be a bit faster
Using htaccess will make it much more simple to organize your files than with lots of folders. Yes, it has an impact on performance, but, unless it's going to be a really high-traffic site, the impact won't be noticeable. It you're worried about performance, keep in mind that bad code can slow down your site a lot more than any rewrite rules.
Is it possible to hide the the url in the address bar of the web browser so that it won't necessarily match the location of the files.
For example, this url:
http://localhost/exp/regstuds.php
You will always know by looking where to find the files in the computer.
Is it possible to distort or disarrange or hide the url in such a way that the location of the files will not be revealed
Yes, if you're using Apache look into using mod_rewrite. There are similar rewrite modules for pretty much all other web servers too.
I hope your sole motivation for doing this is not "security through obscurity". Because if it is, you should probably stop and spend more time on something more effective.
If you are hosting your php on an Apache server, you probably have the ability to use the mod_rewrite utility. You can do this be adding rules to your .htaccess file...
RewriteEngine on
RewriteRule ^RegStuds/ regstuds.php
This would cause http://localhost/RegStuds/ to actually render regstuds.php, but without ever displaying it in the address bar.
If you are on IIS, you can perform the same function using an ISAPI Rewrite Filter.
If you don't have mod_rewrite or an ISAPI Rewrite Filter, you can get a similar result using a folder structure, so you would have a physical path of RegStuds/index.php - and you would never need to link to "index.php" as it is the default file. This is the least recommended way of doing it.
No its not.
Each bit of functionality must have a unique identifier (URI) so that the request is routed to the right bit of code. The mapping can be non-linear using all sorts of tricks - mod_rewrite, front controller, content negotiation...but this is just obscuring what's really going on.
You can fudge what appears in the address bar on the browser by using a front-controller architecture and using forms / POSTs for every request but this is going to get very messy, very quickly.
Perhaps if you were to explain why you wanted to do this we might be able to come up with a better solution.
C.