I am doing on a project and i stumble upon on this while going through some code. I had tried to google but could not find a correct keyword to search regarding this question.
http://www.fruit.com/example.php/red
I am confuse with this and the end of a .php it still have a character /red for example.
What is the purpose of this? Does it have something to do with POST method?
A possible explanation is that the developer is using .htaccess rewrite rules. It is most likely extra data that has to do with the page (such as you mentioned, POST and GET) and it's only done this way to make the URL look prettier or to simplify the URL.
It is also sometimes done this way to make it easier to linkback to this page, especially if the page is being generated based on the data.
A popular place to do this would be blogs. You often see blogs display links as:
http://blog.com/blog/2001/02/23/how-pretty-is-this
when in reality, the request to the server is something like:
/blog.php?year=2001&month=02&day=23&title=how-pretty-is-this
which isn't that pretty and not as easy to link-back to this particular page.
Mr.Xenotype answered your question well but missed one little part.
Does it have something to do with POST method?
No, it deals with the GET method. As he stated, it's pretty much to make the URL look "pretty", and it could actually be a crucial part of a RESTful API (bit advanced for a beginner) if that's how the site was designed.
http://www.fruit.com/example.php/red
Is the same as
http://www.fruit.com/example.php?id=red
Some tinkering with the .htaccess file is required to accomplish this using what are called regular expressions, or REGEX. If you'd like to know more I'll gladly provide some examples.
Adding a "RewriteRule" to the .htaccess file (a server configuration file) can allow the server to change the way the server files/folders are accessed
//probably what the rewrite looks like of the example you provided
RewriteRule .* example.php/$0 [L]
RewriteRules are broken into 4 blocks: action, pattern, rewrite and flag
Action: RewriteRule, essentially just lets the server know we're going to be performing a rewrite
Pattern: .* is our pattern in this example. The "." stands for any character, meaning match any character, and the "*" tells it to repeat this. So, match any character after the rewrite.
The rewrite is the page to perform this on, so example.php is the rewrite. The "$" of this tells it where to start the rewrite. So, we'll start the rewrite at the "$" symbol, and use our pattern. Our pattern ".*" tells us to match any character, in this case, red.
The [L] means last rule. This is it, no more rules to follow. You can use multiple rewrite rules for multiple pages.
So now, instead of using example.php?id=red, the server now knows that starting after "example.php/" to match any characters we supply.
Related
I am developing my login system a bit further to look a lot more professional, and I wanted to know how I could turn get requests into simple links so they look a lot more sleeker?
For example for one of my systems a user can search someones elses profile by going to http://www.example.com/user?user=JimmyJones
Thats all fine and dandy but I don't think it looks very good and many other websites don't have this in their links due to some kind of trick I don't know about, as you can see I have gotten rid of the .php at the end which is done using some very simple htaccess.
But how can I change that link above to:
http://www.example.com/user/JimmyJones
Thank you very much for taking your time to read this and I really hope someone can help me out with my little problem, I assume there is some way to do this in .htaccess?
EDIT:
Here are some websites that do it just about how I would like to do it:
imgur.com/user/example
facebook.com/exampleuser
you make .htaccess file in the root dictionary then start it with
RewriteEngine on
then write your rules, For your example it would be like this
RewriteRule ^/?user/([^/]+)/?$ user?user=$1 [L,QSA]
so a full page would be like this
RewriteEngine on
RewriteRule ^/?user/([^/]+)/?$ user.php?user=$1
just for your example.
In .htaccess assuming the Apache web server with the rewrite module enabled, something like this:
RewriteEngine On
RewriteRule ^user/([a-zA-Z]+)$ user.php?user=$1 [L]
The first line says use the rewrite engine.
The second, says match a url that begins (or rather is relative to the .htaccess containing folder) with the pattern 'user' followed by a slash and matching a pattern of any alphabetic characters until the end of the string (not including additional query parameters).
The L flag basically says job done.
If the .htaccess were in the public root:
//example.com/user/JimmieJones
would map to:
//example.com/user.php?user=JimmieJones
However it will not match:
//example.com/user
//example.com/user/
//example.com/user/JimmieJones/
//example.com/user/Freddy_K9
Note that any existing links in your application:
Visit Jimmie's Profile
Would likely need to be updated. And with the example pattern above, the old style urls (previously indexed/bookmarked) could fail without your existing rule. You may need to adapt the pattern or set up redirects for the old style.
Managing lots of redirects and rewrites can become a headache. I'd advice some attention to your url name spacing. And documenting them.
I reserve first level patterns for aliases/shortcuts/campaigns.
//example.com/slug
I'd avoid that for your user profile urls if possible.
You'll ideally want to aim for consistency and have one-one correspondence for URLs(with associated http method) and resources (canonical urls).
Hey so im working on a website and one part of it allows you to lookup a user based on their name. At the moment i have it using a $_GET request so the link would look like:
http://website.com/p?name=John+Smith
How would i be able to remove that ?name= because i see alot of sites doing things like:
http://website.com/p/John+Smith
how would i achieve this because to my knowladge their arent any other forum request types only Post and Get?
URL rewriting is definitely what you're looking to do. It's well worth playing carefully with it but lots of testing is recommended. With great power comes great responsibility!
Most dynamic sites include variables in their URLs that tell the site what information to show the user. The example you provided is exactly like this.
Unfortunately, a cleaned up URL cannot be easily understood by a server without some work. When a request is made for the clean URL, the server needs to work out how to process it so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, you need to first create a text document called ".htaccess" to contain the rules. This would be placed in the root directory of the server. To tell the server to rewrite a URL pattern, you need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^p/[A-Za-z\+]$ /p/?name=$1 [NC,L] # Rewriting rule here
The NC bit denotes case insensitive URLs and the L indicates this is the last rule that should be applied before attempting to access the final URL.
You can do quite a bit with this one rule, but the specifics extend far beyond the space of my answer here.
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I would highly suggest reading that thorough guide to help you on your quest!
I am very new to .htaccess, I am having some problem with file action.
Example:
http://www.domain.com/upload_pic.php?action=save_pic
I wrote the .htaccess rule like
RewriteEngine On
RewriteRule ^sabc([a-zA-Z0-9!##$-_]*)$ upload_pic.php?action=$1
I want the desired result like:
http://www.domain.com/sabc/save_pic
How can I get the desired result, please correct my .htaccess line.
Change it to:
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
The .+ will capture one or more characters after the / and be captured into $1
There are several other guides on the web already, but to understand it in better way as you are beginner #AMY, I have writen this for you. Hope this will work for you.
Most dynamic sites include variables in their URLs that tell the site what information to show the user. Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7.
http://www.domain.com/upload_pic.php?pic_id=7
The problems with this kind of URL structure are that the URL is not at all memorable. It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). Search engines and users alike get no useful information about the content of a page from that URL. You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). It's a fairly standard URL - the sort you'd get by default from most CMSes. Compare that to this URL:
http://www.domain.com/sabc/7/
Clearly a much cleaner and shorter URL. It's much easier to remember, and vastly easier to read out. That said, it doesn't exactly tell anyone what it refers to. But we can do more:
http://www.domain.com/sabc/user-navnish/
Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. It's an easy URL to remember and to pass to another person.
Unfortunately, the last URL cannot be easily understood by a server without some work on our part. When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "upload_pic.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^sabc/?$ upload_pic.php [NC,L] # Handle requests for "sabc"
A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example).
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
RewriteRule - Tells Apache that this like refers to a single
RewriteRule.
^/sabc/?$ - The "pattern". The server will check the URL of every
request to the site to see if this pattern matches. If it does, then
Apache will swap the URL of the request for the "substitution"
section that follows.
upload_pic.php - The "substitution". If the pattern
above matches the request, Apache uses this URL instead of the
requested URL.
[NC,L] - "Flags", that tell Apache how to apply the rule. In this
case, we're using two flags. "NC", tells Apache that this rule should
be case-insensitive, and "L" tells Apache not to process any more
rules if this one is used.
# Handle requests for "sabc" - Comment explaining what the rule does (optional but recommended)
The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules.
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
Hope this will be helpful for you to understand URL rewriting #AMY.
Brief Explanation
I am trying to come up with a smart way of controlling my web page rewrites with as few rewrites as possible.
So, for example, if you have the following page translations:
'domain.com/test/' => 'domain.com/test.php'
'domain.com/errors/404/' => 'domain.com/errors.php?code=404'
You could have individual rewrites for every page... However, I like to minimise this by instead having one regular expression to suit all. So the regular expression to match the above would be:
^([a-z0-9\-]+)\/?([a-z0-9\-]+)?\/?$
and then the matches would be placed into the URL like so (note that code would be replaced by request on the errors page):
domain.com/$1.php?request=$2
and then you could append the query string...
Obviously in some cases you need individual rewrites, but you can usually minimise this by using clever regular expressions and well structured pages.
My Problem
Similar to the above example, I have pages within my site that are rewritten with this exact regular expression. So the following pages translate like so:
'domain.com/shipments/open/' => 'domain.com/shipments.php?request=open'
'domain.com/settings/user/' => 'domain.com/settings.php?request=user'
etc...
However, for reasons which I will not go into, I also have pages written like so:
'domain.com/shipment/edit'
Notice the it says shipment not shipments! Despite this, I still want this to be rewritten to the shipments page. So the translation would look like this:
'domain.com/shipment/edit' => 'domain.com/shipments.php?request=edit'
I cannot think of a way other than another rewrite that specifies that shipment is an alias for shipments...
One of the reasons I do not want individual rewrites for this is that I am not just talking about shipments, there are many documents that I need to alias, and therefore would rather not go down the multiple rewrite route as my web.config file will get pretty darn large.
I have come up with the following solutions (but obviously I am not happy with them):
Have a page in my root directory called shipment.php and just include the contents of shipments.php
Write rewrites for each page with an alias...
I cannot think of another (better) way of doing this, can any of you?
Since you only have 1 exception, why not add that one exception ?
We will use a "rewrite rewrite technique" (I just came up with that name lol)
RewriteEngine On
RewriteRule ^([a-z0-9-]+[^s/])s*/?([a-z0-9-]+)?/?$ $1s/$2 [NC]
RewriteRule ^([a-z0-9-]+)/?([a-z0-9-]+)?/?$ $1.php?request=$2
Tested on localhost and this RewriteRuleTester.
I've read a lot of sites talking about how to use Rewrite Rules and Regular Expressions, but they all tend to be for specific examples without breaking down every piece of the syntax and actually helping me to learn the concepts so that I can write the type of code I need.
Any help is much appreciated!
I have a url like this http://www.mysite.com/myphp.php?x=1A&y=2B&c=some1&d=some2
I would like this to rewrite like this: http://www.mysite.com/some1/some2
It seems really simple, but as I mentioned, I haven't run into a site explaining the concepts in a way that I could figure out how to make this work.
In this particular example, ?x= is always the same, while 1A varies. The same applies to each of the other pieces of the php string, however, I'm assuming the rewrite rule should handle any number of characters between the .php file and the variables I'm actually interested in.
Thanks again to anyone who can help explain this or provide a high quality resource on both rewrite rules and regular expressions to help me learn it.
A rule like this will work (tested):
RewriteRule ^([^/]+)/([^/]+) myphp.php?x=1A&y=2B&c=$1&d=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, we are saying - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored (backreferences)
As for high-quality resources on mod_rewrite, in the past I've found a few by searching for "mod_rewrite tutorial" (I thought this was a good one for a beginner). They'll mostly be like that, so if there's anything specific you don't understand, get that out of the way before moving forward. I'm not sure how else the syntax can be explained.
Regexes are a different thing, again, many resources out there - including for beginners. Once you get the basic hang of regexes, you can use the Python re page as a reference.
Something like (not tested) added to .htaccess?
RewriteEngine on
RewriteRule ^/([0-9a-z]+)/([0-9a-z]+)/$ /?c=$1&d=$2 [L]
assuming x and y are not of interest.