i've started using modrewrite under php - i wrote an .htaccess file and defined some rules.
but the main question is - how would i do the "opposite thing" of creating the modrewrite-links inside my web?
eg. i'm using this original url inside my website: /search.php?par1=abc&par2=def&par3=ghi
the correspondending modrewrite url would be /go/find/something/here/index.html
so i'd need to replace all html-links in my web using the ugly url with the mod-rewrite url.
what's the easiest way to do this? (function for creating urls, using a database ..)
thanks
As this should be a migration job, only done once, go for search/replace. Using a function or database would mean you have to do the migration for each request to your app. Which seems to be unnecessary.
A function. Simply because that way you can replace what the function does with minimal changes to other parts of the website. This is, for example, the way Drupal does it.
Related
I am having a problem with a PHP script, so the link is like this: page.php?id=1412.
But I don't want to get the data using $_GET['id']; is there a way for the program to recognize the URL like this localhost/1412, and then try to find the 1412 in the id field in database?
If possible how can I avoid the Not Found page ? since the folder 1412 does not exist ?
For all of this kind of matching you need to use either .htaccess files for apache, or similar functionality implemented in the configuration files of nginx/lighthttpd and so on. Basicly, when a request like x/1412 will come, you can match it before being dispatched to the php engine and transform it internally into x?id=1412 . Other solution involves redirecting all requests to a front controller (a script that is serving as a single entry point into the application) and let a url matcher match the route to execute. The latter is implemented in about every big/small php framework.
You could regex the current uri ($_SERVER['REQUEST_URI']). See for creating regex: PHP URL Regex and Parameter
.htaccess file is a solution, you can access this type of url and can send the value to php files using .htaccess file refer this nice article for doing this
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php
I have two questions.
In my webpage (developed by me), I use the GET method to return for example the id of one record that I have in a mySQL database.
For example, one url can be this:
http://www.example.com/example.php?id=1
But in MediaWiki software, they can use this GET method, or one strange method that I don't know, that works like this:
http://www.example.com/example.php/1
How can I do that my PHP script knows that what the id is 1?
And now I have another question. In MediaWiki software, you can do that the index.php file converts to a virtual wiki directory.
Now the final question is: How can I do that in my webpage, the url for this request to my mySQL database (with a PHP script) is:
http://www.example.com/example/1
Thanks.
P.D: Sorry about my bad English. I'm still learning, because I'm from Spain and in Spain the Enslish is not an official language.
You can do that with rewrites (e.g. via .htaccess)
Have a look at this page: http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Apart from using mod_rewrite, if you're using Apache+PHP, you can use the $_SERVER['PATH_INFO'] var to accomplish the same thing.
Use mod_rewrite for the URL - and write a php backend which will parse the url. (fetch the get values)
This link should get you started, http://www.generateit.net/mod-rewrite/
Can I make my .htaccess be generated with php?
I would like to use php to dynamicly create my htaccess file from information from the database.
It would save me the trouble of making a new .htaccess file whenever I change a bit of my code.
You can read and write files with the file system functions, for example:
$data = <<<EOF
RewriteEngine on
RewriteRule …
EOF;
file_put_contents('.htaccess', $data);
But it would be more flexible if you use one static rule that redirect the requests to your PHP file that then does the rest.
Can you? Sure. You can create files in the filesystem with PHP so long as you have permissions.
Should you? Well, if you are only using PHP in "command-line" mode, I suppose it's as good as another scripting language. If you are doing so from a website, I would say this is a bad idea for security reasons. Another technology might be more appropriate (if you originally had XML, then XSLT would come to mind, but you're using mysql, so you'll need some other kind of script).
You definitely can generate your .htaccess with PHP if you had a backend script to do it - say in your admin area. But it seems risky and potentially unnecessary? if something goes wrong, your site will be down until you fix it - that potentially means the page you're using to generate it. Why not just create an interface that generates the new rules for you from a database, then you can have a quick look over and copy them in?
What exactly is it that you're trying to update?
I'm trying to make a clean url for a blog on a dynamic website, but I think that the problem is that I don't know how to plan the website schema.
I read about how to use mod_rewrite and all I found is how to make "http://www.website.com/?category&date&post-title" to "http://www.website.com/category/date/post-title". that's works o.k for me.
The problem is that If my url looks like "http://www.website.com/blog/?id=34" this method won't work as far as I got it.
So, I have two questions:
1. Is there a way to use mod_rewrite (maybe read from a txt file) to read the post title of my blog and rewrite my url by date and post-title?
2. Should I rewrite my website to query the data from one index file in the homepage and use mod_rewrite to write the nice url? should I query also the date and the title of the post instead just the post ID?
mod_rewrite used to rewrite requests and it has nothing to do with urls. You have to change urls by hands.
yes, it's most common practice, to query the data from one index file
no, you can't use mod_rewrite to write the nice url
yes, an id must be present in the url along with title. your engine will just throw title away and use only id to retrieve an article.
Take a look at SO urls for an example
What you're talking about is commonly referred to as routing and lots of examples exist of different ways to do it with PHP. The most common approach uses the frontcontroller pattern, which means in the simple case rewriting all URLs to a single php file and then having that file determine what content to show dynamically based on the URL.
The most popular PHP frameworks (CakePHP, Symphony, Codeigniter, etc.) all have routing code in them which you might be able to use or might serve as inspiration. Alternatively this article covers lots of the basics if you want to do it yourself: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
RewriteMap allows you to do all sorts of dynamic rewriting (text file, script, etc).