Is htaccess file absolutely necessary in router framework? - php

As I understand it, creating an htaccess file creates a scalability concern since every Apache request requires a lookup of things (as I understand it 4 file accesses).
Since I know exactly when my application needs to look up the "retty urls" - is there a way to just bypass having an .httaccess file and somehow look it up via a file access of my own?
Also, I am able to know before-hand, what the pretty url will look like. How can I do the masking most smoothly?
Here is an example:
I made a test page for all trees:
http://www.comehike.com/outdoors/trees/trees.php
And my application can know the pretty urls to create.
But I have a single tree.php file here:
http://www.comehike.com/outdoors/trees/tree.php?tree_id=24
How can I make it take the tree_id (for db lookup) and also look pretty like this:
http://www.comehike.com/outdoors/trees/oak-tree
ps - I am using php

to make any pretty url (like http://www.comehike.com/outdoors/trees/oak-tree), you have to tell apache what part of that url is your script (without *.php in it, how should apache know). "you have to tell apache" meaning you have to change the apache configuration - no way around this. you can do that via .htaccess (with the performance penalty you mention) or elsewhere.
what you can do to minimize changing the apache config is to set up just one main router (eg. http://www.comehike.com/outdoors) and let it handle all paths below it.

You shouldn't be too concerned with the overhead of using an .htaccess file. It won't have much of an effect on your server load.
As for your pretty URL scheme, you can look no further than StackOverflow! Notice the structure of this question's url. It looks like:
http://stackoverflow.com/questions/[ID]/[QUESTION]
The ID as you probably have guessed, is the unique ID given to every question on SO. The question that follows (your equivalent being the tree name) is simply dummy text. Try changing anything after the /4985258/ and retrying the address. It still sends you to this question. That's because the Mod Rewrite that SO uses ignores everything after the id.
In your case, I would suggest a URL structure like this:
http://www.comehike.com/outdoors/trees/24/oak-tree
Using the following htaccess mod_rewrite:
RewriteEngine On
RewriteRule ^/outdoors/trees/([0-9]+) /outdoors/trees/tree.php?tree_id=$1
This will match any URL that had an ID in it and send it off to the tree.php script. It's up to you to add the dummy title text to all your links.
EDIT
If you are truly concerned about the minute performance hit you'll take with an htaccess file, you can always move all the code from your htaccess files to your VirtualHost declaration and then declare AllowOverride None to prevent Apache from searching for htaccess files. You can usually find your virtual hosts in /etc/apache2/sites-available/default in Linux or C:\apache\conf\vhosts.conf in Windows.

Related

url rewriting : add virtual folder in url from value of select box html

With a select box of towns , i must change the url like that :
mywebsite.com/index.php to mywebsite.com/myselecttownvalue/index.php or mywebsite.com/anotherselecttownvalue/index.php
It's possible with an htaccess to manage that ?
Ty for help !
Certainly it is possible to "accept" incoming URLs, that would mean to rewrite them to an internal, existing route which can process the request and respond to it.
Concerning your examples: typically one would leave out the script name, so the URLs would look clean, something like https://example.com/town/myselectedtown and get internally rewritten to something like /index.php?town=myselectedtown so that the selected value is available in the $_GET superglobal variable as $_GET['town']. Take care to URL-encode the town name though, in case it contains blanks or funny stuff...
Take a look at this example setup:
RewriteEngine on
RewriteRule ^/?town/(.+)$ /index.php?town=$1 [L]
The above rule will work in dynamic configuration files (".htaccess") and in the real http servers host configuration.
You certainly could define your URLs without the /town token inside, s you suggested (so just https://example.com/myselectedtown). But that typically raises issues with other URL patterns, since it is never clear if a URL refers to a town name or something else...
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Use slash with get request

I'm in the process of working on an error system for my site (i.e., if MySQL encounters an error, it sends them to an error page). I'm wondering, is it possible to use a "/" instead of "?err=" for a URL?
What I'd like to do is have people sent to the url "/error/404/" but display on page the content at url "/error?err=404". Is there a way to do this with HTAccess, or something of the sort?
My current way is with lots of files and iframes, and it gets really annoying when you have to update one tiny little thing.
Thanks!
What you are looking for is url rewriting. You can set it up using an .htaccess file, given that your installation of apache has mod_rewrite enabled (if not, check this question).
Here is a nice tutorial on how to do it.
Have a try with this:
RewriteEngine on
RewriteRule ^error/(\d+)$ error?err=$1 [L,QSA]
This should not end in a redirection loop, since this requires a trailing number in the URI.
Note that I removed your leading slashes from both the pattern and the result. .htaccess style files work on relative paths.
In general you should always prefer to place such rules inside your http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and the really slow the server down. They are only available as a last option for users who do not have access to the host configuration, for example when using a cheap hosting provider.

How to rewrite profile URL without .htaccess, in PHP?

I am building a router in PHP. I was wondering how I can map /profile?id=3 to /user/3. Is there any way to do this in PHP without .htaccess?
What you could do is create a base entry point which catch all urls like in Symfony, for example.
This entry point could be a app.php file at the root of your project. You can access it via urls like http://yourdomain.com/app.php/user/3.
This entry point will use the URL part after app.php to invoque corresponding controller, extract the user ID and render HTML.
Then, in your apache config, you create a rewrite rule that will prepend all URLs with app.php. http://yourdomain.com/user/3 will be mapped under the hood by apache to http://yourdomain.com/app.php/user/3.
An exemple of such a rewrite rule can be found here.
After that, if you want to support other routes, like /myblog/great-entry or anything else, you can handle them from PHP side. You won't have to edit your apache config ever again, because every URL are catched by your app.php file.
The URL needs to be rewritten from that shortened form, to a query string to be passed to PHP. You cannot accept /user/3 in PHP in any way as it's not going to be populated in the $_GET superglobal array.
You don't need to put rewrite rules in .htaccess files, as they're per-directory inherited. But you still need mod_rewrite.
As you're using PHP, I'm assuming you're not running it on the CLI, so you must be using Apache or Nginx. That being the case, what's the problem running mod_rewrite and putting the rules in the config?

How can I put Pyramid in front of a PHP website using the same webserver?

The scenario is: I current have an old website that runs on PHP. Over time, that code has become hacked up and messy. It's due for a rewrite. However, I don't have time to do that rewrite yet. But I'd like to plan for it in the future.
What I need to do now is add a 'welcome' type page to the website. I'd like to code that in Python using the Pyramid framework.
The old sites URL structure is this:
http://website.com/XXXXXX
Where the X is the short URL id.
What I was thinking of doing was using Apaches LocationMatch config to catch the short URL. Something like:
<LocationMatch "/^([a-zA-Z0-9]{6})$">
This would then redirect the request to the PHP portion of the website. Everything else would be caught by Pyramid.
I'm not sure how to action this. Does Apache have an else type clause for LocationMatch? How would I tell it to serve the PHP files for /XXXXXX matches and send everything else to Pyramid?
Server Notes:
Apache2 + PHP (Debian package)
mod_wsgi 3.3
Python2.7
I am not sure about Apache configuration, but you could use wphp, a wsgi middleware for serving php.
http://pythonpaste.org/wphp/
Use recipes for using AddHandler described in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
Apply the AddHandler and the rewrite rule to your DocumentRoot directory. Then add the SCRIPT_NAME fixup to your WSGI script file.

How can a URL like http://localhost/index.php/articles/edit/1/my-first-article work without an .htaccess?

I don't get this:
http://localhost/index.php/articles/edit/1/my-first-article
This URL is mentioned as an example in the Kohana framework documentation. I poked around in the files of my installation, and there is no .htaccess besides my own one that has nothing to do with that.
So, how can it be that an index.php is called but then, as parameters, the stuff looks like added directories to the URL? That doesn't look "real".
Or is this just how native PHP/Apache/HTTP stuff actually works? As I understand it, / is always telling "hey, a directory!". Makes really zero sense to me... how's that possible? Or do they have somewhere an .htaccess that I just can't see / find?
From the Apache docs:
AcceptPathInfo Directive
This directive controls whether
requests that contain trailing
pathname information that follows an
actual filename (or non-existent file
in an existing directory) will be
accepted or rejected. The trailing
pathname information can be made
available to scripts in the PATH_INFO
environment variable.
For example, assume the location
/test/ points to a directory that
contains only the single file
here.html. Then requests for
/test/here.html/more and
/test/nothere.html/more both collect
/more as PATH_INFO.
So I assume this setting is enabled somewhere like in httpd.conf. Note that this, like mod_rewrite, can be enabled/configured in a number of places - see here.
In PHP you can get the data after the filename with the $_SERVER["PATH_INFO"] variable. This allows you to basically GET information without having to use GET variables, which means Google and co will think you're using static pages. This is basically an alternative to mod_rewrite which is often enabled while mod_rewrite is more often not enabled.
This may be obvious to you, but it wasn't immediately to me, this doesn't work correctly on index pages unless you use the filename. For instance, http://example.com/test/my/get/params will not work, while http://example.com/test/index.php/my/get/params will.
AcceptPathInfo is turned on for your server. :)
I'm not using Kohana, so I don't know if my method will be of any use, but when a server doesn't support .htaccess files (or rewrite rules) my 'framework' generates URI's like this:
http://www.domain.com/?/articles/edit/1/my-first-article (notice the ?)
It's a similar method used by the Frog framework, just parse $_SERVER['REQUEST_URI'] (or $_SERVER['HTTP-X-REWRITE-URL'] on windows servers) and explode on the '/'.
This method is completely rewrite independent and still generates more-or-less SEO friendly URI's
Hope it's of any use to you.
Probably not the case here, and certainly not recommended, and probably not the right answer...
BUT ...
I've seen people use 404 pages to parse the request and then include the right page with that information.
See PATH_INFO in CGI Environment Variables.

Categories