This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to create friendly URL in php?
I have read a lot of content on Google about turning
profile.php?id=24 into something more user friendly.
How would I go about redirecting a link like
www.site.com/profile/username
to
profile.php?id=userid (find from username)
Thanks
This can be achieved with the Apache mod_rewrite RewriteEngine. An example .htaccess:
RewriteEngine On
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1
You can do that with apache rewrite rules.
Apache will internally rewrite a URL like /profile/username to profile.php?username=username.
Example: (place this in a .htaccess in the same directory than profile.php)
RewriteEngine On
RewriteRule ^/profile/(.*)$ profile.php?username=$1
If you profile.php script doesn't accept a username parameter, you could also include the user id in the user friendly url:
RewriteEngine On
RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2
This will rewrite urls like /profile/username-id to profile.php?id=id
See apahe mod_rewrite documentation.
This is done by creating a RewriteRule in a .htaccess file, of by defining rules in httpd.conf.
This works on Apache. I'm, not sure how to do this on other servers.
When your users sign up, use a PHP or CGI script to make a file called "Username.txt" and store it in a folder called Profiles (as you have it). And inside the text file, make it generate the number (counting up or hashed?) Or use a rewrite service in Google or Apache.
Related
After reading tons of SO questions, asking friends and so one, I'm coming here with a strange issue regarding Apache mod_rewrite.
I'm trying to catch http://api.server.com/.../results.php?id=X URL though a RewriteRule.
Quite simple you'll say, I know it, my .htaccess file content is :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1
results.php is quite simple for debugging reasons, looks like
var_dump($_GET);
But this script always return array 0 { }
Shall I specify that I've already tried to clear the flags, and change the (.*) class by others, without effects.
Thanks for your help.
You will need to disable MultiViews option here:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Your rewrite rule does not match the URL you are using (http://api.server.com/customer/2/results.php).
The correct URL according to your rule and setup is:
http://api.server.com/customer/2/results/123
However, you mention having placed everything in the /2/ folder. If 2 is the ID you are trying to get, it cannot work -- URL rewriting only works with non-existing paths.
Instead you should place your .htacess and results.php file in the customer folder, and use the following URL:
http://api.server.com/customer/results/2
Or change your rule and URL to:
RewriteRule ^([0-9]+)/results$ results.php?id=$1
http://api.server.com/customer/2/results
This question already has an answer here:
Friendly Url .htaccess
(1 answer)
Closed 8 years ago.
my url is
http://altikalti.com/down.php?folderpath=53/143&imageid=29&file=17Mar2014-murzuq-band-ghat-festival-libya(altikalti.com).jpg&new
output url:
http://altikalti.com/53/143/29/17Mar2014-murzuq-band-ghat-festival-libya(altikalti.com).jpg
How can i do.. if any changes in panel setting then you can also suggest me
thank u for reading...
Have a good day
Create .htaccess file into root folder (e.g. http://altikalti.com/ location)
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)/(.*) http://altikalti.com/down.php folderpath=$1/$2&imageid=$3&file=$4&new [L]
If you're not in production environment, you can check if mod-rewrite is enabled by
RewriteEngine On
RewriteRule ^(.*) http://www.google.com/ [L, R=301]
If it work every request should be redirect to google.com, which mean the first script should work and do the job for you.
This question has been asked several times already.
By searching with the keywords friendly url htaccess you can find this discussion that give an answer on the subject :
Friendly Url .htaccess
Or this one, especially for images :
Friendly URL for images
You can still follow this blog post for more complete examples :
http://surefirewebservices.com/tutorials/friendly-url-tutorial-pt1
I want to have a single PHP file that takes care of multiple URLs in a subdirectory.
For example, my site is http://www.startingtofeelit.com/. I want one php file, say, called playlist.php which would handle when a user goes to http://www.startingtofeelit.com/playlist/101 or if they go to http://www.startingtofeelit.com/playlist/142 etc. I want to be able to strip the number (101, 142 in my example urls above) to use as a variable (the playlist ID), so I can display the correct playlist.
I know that I can create an index.php in my playlist subdirectory and use GET variables like http://www.startingtofeelit.com/playlist?id=102 and get the ID that way, but this is much sloppier looking and I'd like to be able to know how to do it the other way.
My site is built on WordPress, but I don't think this should make a difference in any way.
Well, you cannot achieve this with PHP alone.
If you use Apache, you can use .htaccess
If you use IIS, you can use URL Rewrite
The basic idea behind those modules is to mapping from one URL to another URL. For example: you would want to map from
http://www.startingtofeelit.com/playlist/142 =>
http://www.startingtofeelit.com/playlist.php?id=142
You can express the URL mapping in regular expression. For example, in .htaccess (Apache). You can write like this
RewriteRule ^playlist/([0-9]+)/?$ playlist.php?id=$1
Noted that, you need to have .htaccess file in your website directory. Since, you are using Wordpress, chance that you have existed .htaccess is high. You can simply append that line of code to existed .htaccess
The following is an explanation of the regular expression:
^playlist/ # any URL start with playlist/
([0+9]+) # following by number, and store it as $1
/?$ # end with or without /
Mapping to
playlist.php?id=$1 # where $1 is taken from the matched number from our pattern.
This is usually handled in a way similar to what you already tried. However, it's common to use a re-writing script so that your application will accept a clean URL such as:
http://www.startingtofeelit.com/playlist/142
...and re-write it for your application as such:
http://www.startingtofeelit.com/playlist?id=142
For example, if you're using an Apache web server and have the mod_rewrite module installed and enabled, you can use the following snippet in an .htaccess file and use your GET parameter as you indicated you already know how to do. Other popular web servers have unique URL re-writing modules that will let you do the same.
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite this:
# http://www.example.com/somepage/1
# ...into this:
# http://www.example.com/somepage?id=1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mod_rewrite, php and the .htaccess file
Would like this to happen in the .htaccess file.
to rewrite
index.php?pagetype=name
to
/pagetype/name/
do this for all pagetypes so
?user=name
?post=number
?life=sucks ... etc
but would like to exclude
/css/
/images/
/js/
/php/
/template/
so the variable links still work.
I am guessing this should work but I want it in it's most simplest form so I don't have to change the .htaccess file everytime I add a pagetype.
I did a search for this but didn't find anything helpful.
The site will only have one physically accessed file: index.php then the rest are processed via $_GET commands
Just for the example you specified, the simplest approach would be:
RewriteCond %{REQUEST_URI} !^/(css|images|js|php|template)/
RewriteRule ^(\w+)/(\w+)/?$ index.php?$1=$2 [QSA,L]
(The RewriteCond could be written as negative lookeahead in the Rule instead of course.)
See also the mod-rewrite tag wiki for further howtos.
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