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?
Related
I am trying to find out how websites like Imgur, MEGA and such are able to do this:
https://imgur.com/a/SbmNz (emphasis on SbmNz)
The SbmNz bit is dynamic between images or files and I guess it is a kind of $_GET. And I was just wondering how you can do this without the usual ?name=value way.
you can use any MVC or just write a htaccess rule for it , for instance, you can use laravel to pass variables along with url
for laravel refer URL Generation-Laravel5
to write .htaccess refer USING .HTACCESS REWRITE RULES
You can do with Apache rewriting module by changing .htaccess (Create .htaccess file in your folder)
For example if you have this snippet
RewriteRule ^play/([^/]*)$ player.php?id=$1 [L]
When user visits www.example.com/play/Trg4 in backend request is actually handled for www.example.com/player.php?id=Trg4
Then you can get the id with $_GET['id']. it means there is no change with php code. it is completely done with .htaccess
Make sure you enabled rewrite_module of your Apache server
I am trying to write a content management system and have hit a snag while trying to develop seo friendly urls. I am using php to handle urls, however I have a problem when I try to get the REQUEST_URI for more than one depth level. I am trying to avoid using .htaccess to handle this, because I would like the system to be fairly easy to set up on IIS/nginx/etc also and do not want it to be apache dependent any more than is necessary.
I have in my htaccess file
FallbackResource index.php
and then in my php I have a class that handles the REQUEST_URI slug by checking to see if a record exists in the mysql database. This works fine if the request is something like
http://example.com/foo
however throws an internal server error if the request is
http://example.com/foo/bar
This seems to occur even if I have a completely blank index.php, so I suspect the answer must be at the htaccess level. How can I get my system to handle multiple REQUEST_URI depth levels? Do I need to use a mod rewrite regex or is there a less apache dependent solution?
My bad, I needed to change my .htaccess rule from
FallbackResource index.php
to
FallbackResource /index.php
The missing slash was causing the error. -.-
I'm use to working with $_POST and $_GET arrays in PHP scripts. I'm trying to learn Backbone (specifically the Backbone.sync function) and apparently it assumes that you'll set up your server so that example.com/student/32053 retrieves the student with id 32053.
How do I set up my scripts to do this?
Doesn't example.com/student/32053 load up index.* from the "32053" subdirectory of "student"?
On an Apache server, use the mod_rewrite module. In you .htaccess file, just specify a simple rule like:
^data/(\d+)$ data.php?id=$1 [L]
Doing this, when calling example.com/data/100, apache will internally call example.com/data.php?id=100.
Let me know if you need a similar example for Microsoft IIS.
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.
Currently, my page URLs look this this:
http://ourdomain.com/articles/?permalink=blah-blah-blah
I want to convert these to:
http://ourdomain.com/articles/blah-blah-blah
How can I accomplish this using PHP but not with .htaccess?
How can i accomplish this using php but not with .htaccess..
You can't. You will need to tell the web server how to deal with URLs that don't physically exist. In Apache, that is done in the central configuration or in a .htaccess file.
If your server already happens to have AccepPathInfo On, you can try having URLs like
http://ourdomain.com/index.php/articles/blah-blah-blah
which will redirect to index.php and have articles/blah-blah-blah in the $_SERVER["PATH_INFO"] variable. This method is known as "poor man's URL rewriting" because you can't get rid of the index.php part in the URL. If the mentioned setting is turned on (I think it is by default), you may be able to do this without using a .htaccess file.
You can achieve this without mod_rewrite if you have access to the server configuration. Assuming you're using Apache, the first thing you would need to do is turn the MultiViews option on on your document root (ie. add Options MultiViews). Now copy your /articles/index.php to /articles.php (so put the script in your document root and rename it), and adapt your script so it reads $_SERVER["PATH_INFO"] to fetch the correct page (this of course relies on having AcceptPathInfo On).
MultiViews will make sure that the articles.php script is called when you provide a /articles/blah-blah URL.
I don't think you can easily do it without altering .htaccess. You'll most definitely need to use mod_rewrite. See the answers here for more info:
Special profile page link like www.domain.com/username
It is possible to do it in PHP, without modifying .htaccess
Just write following code in either index.php or default.php
<?php
if (isset($_GET['permalink'])) {
header('Location: '.urlencode($_GET['permalink']));
}
?>
It works because when you type following URL:
http://ourdomain.com/articles/?permalink=blah-blah-blah
The filename is not specified.
So, the server looks whether "index" or "default" file is present in the specified directory.
Consider file index.php is present, so server will call:
http://ourdomain.com/articles/index.php
with blah-blah-blah in GET variable permalink
The PHP code checks if permalink GET variable is present, and redirects using header() method.
EDIT: added urlencode() to do input validation