Before I go ahead and ask you my question, I understand that there are many methods on the internet to accomplish this sort of thing. I understand also that this process is called rewriting urls. However, since I am not a proffesional at coding, the process of these things seem very daunting and hard to understand. So, I thought I'd ask the StackOverflow community for help!
Here's what I'd like. Currently, I have 2 main files.
index.php and php.php.
My site allows users to enter a username and get information for that username. index.php holds the form input, and php.php handles the actual PHP Code. Once a user has submitted the username, the URL at the end will look like this;
http://mcspy.info/php.php?username=_scrunch
What I'd like the URL above to look like is:
http://mcspy.info/player/_scrunch
Could somebody here please guide me and explain in an easy manner to understand, how I would go about doing this? Preferably using PHP! Thanks a lot!
Well it would be much easier to do this with a .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^player/(.*)$ php.php?username=$1 [L,NC]
The easiest and cleanest way is with .htaccess files, but if you don't want to, you can use PHP, what I'm not sure but this would help URL rewriting with PHP
Related
I have a problem and I don't know if it's possible but I'm going to ask here.
I have rewrite rules like this:
RewriteRule ^login$ index.php?page=login [NC]
and I want to do something like this, if you add "/success" then the url will look something like this:
"mydomain.com/login/success"
and that will add a $_GET variable to the url and the original url will be:
"index.php?page=login&status=success".
I want to be able to add /success to every page that I have, example: mydomain.com/register/success and so on..
Thanks!
Allright, first thing:
This is not how URI Rewrite works. Sorry man but you really need to study a bit more on the topic.
To solve your issue you need to:
Get the URI using PHP Global Vars
Process it and extract your parameters
There is no way .htaccess will be able to do what you are looking for. I don't usually post my stuff, but i suggest you to read some of the source code of a project i've been working on a few months to understand how routing works. URLParser Example here
Unfortunately to properly understand my code you have a minimal notion of MVC. Hope it helps!
I'm working on this application and I'm having a little issue regarding htaccess mod_rewrite.
Before I explain what I want to do I'll show you what I've done so far:
RewriteRule ^client([0-9]*)$ index.php?clientID=$1
This essentially allows me to use url.com/client5 instead of url.com/clientID=5, that works perfectly. But lets say I would like to actually use GET variables and do something like url.com/client5?page=main, would it be possible to achieve that, if not anyone could suggest an alternative approach?
Thank you!!!
Please bare in mind that the .htaccess file is stored in the root dir meaning it will affect all of the files.
You can do it with [qsappend].
RewriteRule ^login.html$ ?action=login [qsappend]
You can call it like. .com/login.html?page=main
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 was not sure how to word this, so I am going to try to explain as well as I can. I have written a plugin system in PHP for a CMS. However, the plugins are contained within their own folder, a page that came with a plugin might look like this:
http://www.mysite.com/plugins/forum/index.php
However, this looks ugly and obviously exposes the site structure of the plugin system. So far I have been getting around this problem like so:
http://www.mysite.com/page.php?id=forum
but that is still kind of ugly even though its a little cleaner than the latter. However, I was wondering if it is possible to create a "dummy" page which is actually just an alias for the page I want. So
http://www.mysite.com/forum.php points to http://www.mysite.com/plugins/forum/index.php
While I can create a page in the root forum.php manually, I was wondering if there was some way to automate it. In other words, when the user calls forum.php which doesn't exist, I have a 404 page hold a piece of PHP code which figures out what is being called (throwing a 404 if its an unknown page call). In other words, if I request forum.php, a piece of code consults the database to find the linker and if it does, spits back the contents of the page under that specific URL.
I hope this is not too confusing. Let me know if its a bit hard to understand the idea... I would greatly appreciate any help!
If you are using apache, the solution is to use mod_rewrite (.htacces)
.htaccess tricks and tips
The idea is to redirect internally to your plugin when the user accesses
http://www.mysite.com/forum.php
Ex: you place this in your .htaccess file in your webroot
RewriteEngine on
RewriteRule ^forum\.php$ /plugins/forum/index.php.php [QSA]
I need to learn how to do URL rewriting for a site to get pretty URLs...
I have a general idea of how it works through the 50 tutorials I read. But none of them really clicked and never managed to make it properly work.
Do you know of any tutorials that are good for me to start?
Here is an example of how I want to use it:
Here is the basic link
http://www.dico2rue.com/dictionnaire.php?idW=675&word=Resto-basket
I want it to become like this:
http://www.dico2rue.com/dictionnaire/675/Resto-basket
But when I did that, all my links (like for CSS sheets, images,...) didn't work. Do I need to full linking (which apparently slows your site down)?
Thanks for your help.
UDPATE
There are 2 options for the links
PAGE = ?page=1
WORD = ?idW=45&word=google-friendly-url-part-here
The code in htaccess is
RewriteRule ^dictionnaire.php/p([0-9]+)?$ dictionaire.php?page=$1 [NC,L]
RewriteRule ^dictionnaire.php/([0-9]+)/([a-z])?$ dictionaire.php?idW=$1&word=$2 [NC,L]
I hope this helps.
Have a play with something like CodeIgniter, Wordpress, or Drupal. You can see how the URLs are formed and how they map to the contents of .htaccess.
This mod_rewrite cheatsheet is very useful.
Given that your stylesheets are already referenced absolutely (they include http://www.dico2rue.com/) it's probably because you're telling all your images, stylesheets and JavaScript files to go through index.php. Instead you need to tell .htaccess to leave physical files alone, and only parse other URLs.
Post what you have in your .htaccess file here and I'm sure we can help fix it.
Check if you're requesting a existing file using a (negative)RewriteCond using flags -f and/or -d. Used in many tutorials, check the topmost results at any search engine.