I would to know how one is able to append a username directly to a site url without having to put it within a query?
Eg
www.myspace.com/micheal
instead of
www.myspace.com?name=micheal
Without having to create a new folder for the user so that when the url is typed including the name, the surfer is taken directly to the user's profile.
Thanx
If you're using Apache, which, using PHP, you most likely are, look into mod_rewrite. This lets you do things like this, where www.myspace.com/micheal would be translated internally to www.myspace.com/?name=micheal before being sent to the scripts.
Take a look here http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html for the documentation on how to use it.
For the Apache web-server .htaccess file with the following code will do the thing.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?name=$1 [QSA,L]
This is called url rewriting, and is handled by mod_rewrite on Apache servers.
A rewrite rule takes the incoming uri, parses it and rebuilds it into what the script needs to run.
A very simple example:
RewriteRule ^michael$ /?name=michael$
There's lots on Google when you know where to look. Start here:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
As everyone has pointed out you want URL Rewriting.
If you are using IIS rather than Apache, there are still a couple of options.
Free Option - Ionics Isapi rewrite filter
Commercial Option - Isapi_Rewrite
I think you might be referring to "Pretty URLS" which is generally setup on a web server level using something like Apache mod_rewrite:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html
Related
I store data in text file.
And when user enter in address bar something like
my_syte.com/aaa - (without extension)- I need to file_get_contents aaa.txt file
my_syte.com/bbb - I need to file_get_contents bbb.txt file
Please advise the most powerful way of do it. Apache server.
Thanks
On Apache servers you can use mod-rewrite in .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z]+)$ /$1.txt [L]
if your files can contain - or _ or numbers then use:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.txt [L]
On nginx servers it's more complicated but some of them works with .htaccess. On other servers there may be entirely different approach. It's hard to help you without more informations.
As you said it's Apache, then use examples above. Either edit or create .htaccess file on your webroot (directory which is accessed by domain). First check if it were there (could be hidden) and if it exists then only edit it (add lines at the top).
If it doesn't exist, then create one by yourself.
Can you please give us some insights about your server? Apache nginx?
In Apache, you can achieve that with url rewriting.
Enable mod_rewrite in apache
Put the following line of code in .htaccess on the same location of my_site.com/
RewriteEngine on
RewriteRule ^/foo$ /foo.txt [PT]
to make it generic
RewriteEngine on
RewriteRule ^/*$ /foo.txt [PT]
Maybe I am wrong in sytax based on your specific server configuration. You need to make the best possible regular expression for this case.
I have a web page with the following parameters.
http://www.somesite.com/community_details.php?comm_id=233®ion_id=2&city_id=40
I would like it changed to this
http://www.somesite.com/virginia/fairfax/some_community/
How do I call db to return region, city, community for a url rewrite in .htaccess?
It's not particularly efficient, but you CAN use a RewriteMap to use an external txt/dbm/program to let mod_rewrite do lookups and rewrite based on the results.
RewriteMap pretty-community prg:/path/to/some/shell/script
RewriteRule community_details.php?(.*) ${pretty-community:$1}
The captured query string from the community_details.php script would be passed to the specified external script on its stdin, and the script replies with the rewritten url via its stdout.
Note that the script is started ONCE when Apache first fires up, and then essentially runs in daemon mode, communicating with Apache for every rewrite performed. Since PHP isn't particularly suitable for writing daemons, you might want to do this program in some other language.
I think; you can't
If your link is http://www.somesite.com/virginia/fairfax/some_community/
you could change it with htacces as http://www.somesite.com/community_details.php?comm_code=samo_community®ion_code=virginia&city_code=fairfax
and take id of codes from database to use in php.
Reading this is a good start:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap
You create a regex for your pattern, then write an external Perl/PHP whatever script that implements a hash lookup against a MySQL table and returns it to Apache.
Well, it's quite easy.
just route ALL virtual requests to php scritp which will query a database.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [QSA,L]
and you will have requested path in $_GET['request'].
split it up using explode and get your parameters from db.
You can't.
What you can do is have community_details.php do the database lookup, then call die(header("Location: blahblahblah"));. But then you would have to process that back using .htaccess and everything else, so it might be easier to just not do that at all - depends on how important such URLs are to you.
I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Remove .php extension with PHP
What's the best way to get rid of .php suffix in url strings so they look pretty?
Thank you in advance;-)
Use apache mod_rewrite (rewriting rules)
http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
Make sure your apache installation has mod_rewrite enabled (will be in httpd.conf, or one of the files linked there, mods-enabled or such) and look into how routing works in cakePHP.
Couple of tips - the rewrite rules are found in the .htaccess files (make sure you don't have a unicode BOM if the server gives a 500 error) and if you do find you need those $_GET paramters, [qsappend] on your rewrite rule should pass them along. If you still get 500s the compilation errors on regexes can be found in apache's error log, invaluable for debugging.
Might be easier to do a simple project with mod_rewrite first, to learn how it works, as the combination of rewrite and routing in cake can get pretty complex pretty fast.
Options +MultiViews
in the Apache configuration.
Here is a gentle introduction into mod_rewrite.
The best way to do so (at least for me) is:
Use just one file to receive all request. In most of the cases it will be the index.php file.
Then, use mod_rewrite rules like this:
:
RewriteEngine On
RewriteBase /the_base_dir_of_your_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /the_base_dir_of_your_app/index.php [L]
Then, you can analize the URL using functions like basename($_SERVER['REQUEST_URI']); in order to decide what to do.
Use mod_rewrite - or start using ASP.NET MVC 2 :)
If you use a framework, like CakePHP (or any other) it will do it for you. For free. Right now.
.htaccess:
Permalinks
RewriteEngine on
Remove www
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
Links
RewriteRule ^faq$ /faq.php [L]
RewriteRule ^donations$ /donations.php [L]
RewriteRule ^contact$ /contact.php [L]
so they look pretty?
Beauty is in the eye of the beholder. It depends when you consider 'pretty'. A lot also depends on how much you want to get away from the conventions that make a working system possible and the constraints in terms of reconfiguring your site.
While others have mentioned using mod_rewrite, or URL parsing or other such approaches I'm not a fan of these - in addition to being very specific to the type of webserver the code is running on they also break the simple 1:1 mapping beween paths in URIs and paths on the webserver's filesystem.
You could just substitute '.php' with an extension of your choice...but that hardly meets my interpretation of 'pretty'.
The approach I take is to have every script (or at least every script with is intended to be entry point to generaeing a web page) is named as index.php and exists in its own uniquely named directory. The main reason for doing this is nothing to do with making the URL look nice but rather to make the codebase more manageable - I also have strict standards about the naming and placement of include files.
HTH
C.
I have a simple html page that only uses PHP in two places
<?php preg_replace('/(www\.)?([^.]*)\.(com|info)/', '${2}', $_SERVER['HTTP_HOST']); ?>
<?php echo $_SERVER['HTTP_HOST']); ?>
In page is loaded on multiple domains, and I just want to display the host name as text in some other static content
I'd like to remove the need for PHP completely, but not repalce it with another full blown compiler or interpreter. I'd like to avoid using javascript. I can live without being able to do a regex to get the second level of the domain name, but would still like that option. Do I have any options for doing this via a simpler apache module than mod_php?
Theres nothing wrong with mod_php, I'm just seeing if I can minimalize the needs of this website I am working on.
I’d combine both mod_rewrite and SSI. Set an environment variable with mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.(com|info)$
RewriteRule ^ - [L,E=HOST:%2]
And then access that information in SSI with:
<!--#echo var="HOST" -->
I havn't tested it buy you might be able to use htaccess and do a rewrite like this:
RewriteRule (.*) $1?httm_host=%{HTTP_HOST} [L]
I don't know for sure that the %{HTTP_HOST} variable is available in a rewrite but it may work. You may need to use a condition to check for the ? in the URL.
How about JavaScript? You only need two small changes on the site, that can easily be done. So you can plainly serve static HTML files. And to get the domain, you can use:
var domain = window.location.hostname;
Cheers,
Edit: To use mod_rewrite: You will have to set up two identical HTML files, with the correct domain in each. Then you can deliver them via
RewriteCond %{HTTP_HOST} .+\.com
RewriteRule index.html index.com.html [L]
RewriteCond %{HTTP_HOST} .+\.info
RewriteRule index.html index.info.html [L]