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
Related
I am building an application using PHP and was wondering how I would goo about turning a URL like "http://localhost/app/index.php?function=viewAssignments&id=14 into something like http://localhost/app/assignments/14 without having to create a folder called assignments and a page for every assignment. (That's obviously unrealistic).
I already learned how to change the htaccess to make individual PHP files be accessible without actually typing .php at the end, but is there some way to make clean URLs like how I mentioned?
Thank you in advance for your help!
Assuming your server supports mod_rewrite, then you can accomplish this simply by creating a file in your directory called .htaccess and copy this code into the file,
RewriteEngine On
RewriteRule ^app/([^/]*)/([^/]*)$ /app/index.php?function=$1&id=$2 [L]
Which will take something like this,
http://localhost/app/index.php?function=viewAssignments&id=14
to look like this,
http://localhost/app/viewAssignments/14
Say I want to have URIs like,
www.domainname.com/horses/italian/
www.domainname.com/cats/persian/
The respective APIs are,
GetHorseDetails.php
GetCatDetails.php
They are located under /apis/fetch/.....php
The problem I am facing is that I am not being able to frame the RewriteRule correctly.
When I'm placing the APIs in the root folder and using,
RewriteRule ^(.*)$ GetHorseDetails.php/$
I am able to do a GET domainname.com/horses/italian/ and hit the API.
Summing up,
1) I need to bind the parameters.
2) I need to be able to hit the APIs by using relative paths in .htaccess
Technically,
domaninname.com/horses/italian should hit root/apis/fetch/GetHorseDetails.php
Any help on this would be highly appreciated because I tried digging a lot but to no avail.
Regards.
No quick answers, so I decided to forget all what I accumulated and simply experiment.
Viola, This just worked!
RewriteRule ^horses/italian/?$ apis/fetch/GetHorseDetails.php [NC,L]
Apparently the issue was with the relative path. There's no need to put anything like "./" before the name. And there's no need to use Alias to map the URL to the file system.
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
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 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.