Forum Moderators: phranque

Message Too Old, No Replies

url rewriting

         

Bigjohn

10:14 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



I'm trying to figure out how to use .htaccess file to rewrite my URLs to make them more search engine friendly...

So, if I have a URL that would appear like this:
[somesite.com...]

How would I rewrite it so that it just went:
[somesite.com...]

Any help?

coopster

12:49 am on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, I would go just the opposite, which I think is what you meant, change a "directory" to a query string. For example, all your links would look like a directory structure and you would change them to a redirected query string on-the-fly:
change this... 
http://somesite.com/pagename
to this...
http://somesite.com/index.php?page=pagename
Well, if you are using Apache for your web server, you are going to want to get more familiar with mod_rewrite [httpd.apache.org] and the URL Rewriting Guide [httpd.apache.org]. The Apache forum [webmasterworld.com] here at WebmasterWorld is also a better place to post this type of question for future reference. If you are using Apache, here is an example that you could use in a per-directory override file (.htaccess):
RewriteEngine on 
RewriteCond %{REQUEST_URI} ^\/([^\/]+)/?
RewriteRule .* index.php?page=%1 [L]

Bigjohn

2:12 am on Jul 6, 2004 (gmt 0)

10+ Year Member



is that right? My code sends:
page=?whatever

I want the user to see
mysite.com/whatever

sorry, but this is so confusing... especially with the 'regular expressions'... Arrgh.

coopster

2:20 am on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Right, you have to make your links say the same. For example:
<a href="http://mysite.com/whatever/">mysite</a>

Bigjohn

3:51 am on Jul 6, 2004 (gmt 0)

10+ Year Member



how do I do that when my 'url' is based on a PHP _GET statement?

coopster

7:59 am on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's the beauty of mod_rewrite. You can give your links a "fake" url which the Apache server module will rewrite following the instructions you have given. In this case, you setup your "fake" url as I just showed. Then when a user requests that page, the directions given in the
RewriteCond
and
RewriteRule
directives are processed and the user is sent the PHP
$_GET
url you specified in the
RewriteRule
.

stargeek

8:49 am on Jul 6, 2004 (gmt 0)

10+ Year Member



This may help:
[code]
<?php
function processURI() {
$request = explode("/",$_SERVER['REQUEST_URI']);
$count = count($request);
for ($i = 1 ; $i < $count ; $i++) {
$values["arg".$i] = $request[$i];
}
return $values;
}
?>
[\code]

By calling this function at the top of a page, the array $values contains the URL that was used to access the page, divided by slashes.
You can then login to your server and create a link (do a man ln for more info) to a static "file" in your web directory, for example "directory", from your php script.
When a user accesses www.yourdomain.com/directory/widget/1.php
your php script will be called and $values will contain the the values widget and 1, along with the domain name and "directory". You can then use these values in the script with minimal modification by overwriting the needed GET vars.
This solution is easy, IMHO, if you are better with PHP than apache.

Bigjohn

2:27 pm on Jul 6, 2004 (gmt 0)

10+ Year Member



my index page is simple:

<?php
require 'head.inc';
if (isset($_GET['class'])) {
$class="";
}
if (!isset($_GET['page'])) {$content= 'page1.inc';}
else {$content= $_GET['page'].'.inc'; $class="tc";}
include 'containers.inc';

include $content;
include 'footer.inc';
?>

a user selects a link in the menu (the menu is included in the containers.inc file)
This is a link example:


<li><a href="index.php?page=support" id="spbtn">Support</a></li>

and so the url ends up looking like:

http://somesite.com/index.php?page=support

All I want to do is figure out the easiest way to have the url display as


http://somesite.com/support

John

Timotheos

4:38 pm on Jul 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Bigjohn,

Ya gotta trust coopster on this one. Just try it out and see how it works.

First change your menu links to use the directory stucture you want the user to see.
<li><a href="http://www.somesite.com/support" id="spbtn">Support</a></li>

Now make a simple text file called .htaccess with the following in it and put it in the same directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/([^\/]+)/?
RewriteRule .* index.php?page=%1 [L]

Now click on the link.
You'll see [somesite.com...] but the script will actually use [somesite.com...]

It'll work like a charm.

jdMorgan

5:54 pm on Jul 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To prevent recursion (looping), I'd suggest:

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteRule ^([^/]+)/? index.php?page=$1 [L]

Note that slashes in the pattern need not be escaped for use in mod_rewrite.

You may also wish to exclude other filetypes from being redirected, depending on how your site is configured. For example, to exclude jpeg image requests from being redirected, you could use:


RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteRule ^([^/]+)/? index.php?page=$1 [L]

Jim

timster

7:51 pm on Jul 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great thread. I've been meaning to pick up this stuff for a while, so couldn't resist joining the fun.

I tried copying off of JD...

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteRule ^([^/]+)/? index.php?page=$1 [L]

...but now $1 contains "index.php" -- it's not supposed to, I reckon. Little help?

coopster

8:20 pm on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's part of the recursion repair-work (thanks for noting that, jdMorgan), drop the beginning anchor on the RewriteCond:
RewriteEngine on 
RewriteCond %{REQUEST_URI} !/index\.php
RewriteRule ^([^/]+)/? index.php?page=$1 [L]

jd Morgan said: Note that slashes in the pattern need not be escaped for use in mod_rewrite.
hehe, yep, I even missed one anyway; too much PHP regexing lately ;-)

stargeek

1:03 am on Jul 7, 2004 (gmt 0)

10+ Year Member



You could edit your code like this:
<?php
function processURI() {
$request = explode("/",$_SERVER['REQUEST_URI']);
$count = count($request);
for ($i = 1 ; $i < $count ; $i++) {
$values["arg".$i] = $request[$i];
}
return $values;
}
$values = processURI();
$_GET['class'] = $values[1];
require 'head.inc';
if (isset($_GET['class'])) {
$class="";
}
if (!isset($_GET['page'])) {$content= 'page1.inc';}
else {$content= $_GET['page'].'.inc'; $class="tc";}
include 'containers.inc';

include $content;
include 'footer.inc';
?>
This way you don't have to mess around with apache.

WhosAWhata

2:45 am on Jul 7, 2004 (gmt 0)

10+ Year Member



stargeek PHP can't do the same kind of redirects that we're talking about, Apache is necessary

stargeek

4:57 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



actuallly, WhosAWhata, any redirects that can be done in apache can be done with PHP and the header command.
However no one has talked about a redirect, we are simply listing ways to rewrite a simple URL. thanks -Dan

jdMorgan

5:45 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before we get all wrapped around the axle here, the original post asks specifically about .htaccess.

The reason that this function needs to be done at the server level and not via php is that the problem to be solved is this: How do I deliver a request for a "page" to my php script so that I can process it? The reason php won't work in this case is that it can't work if it has not been invoked yet. IOW, you have to deliver the request to the php script before it can do anything.

This can be done using a "smart" function such as mod_rewrite to selectively rewrite incoming URLs to the script, or simply by declaring the script as the handler for all http requests. But either way, it requires a server-level directive.

Jim

stargeek

11:07 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



You can use the LN command at the *ix prompt to create a file such as "directory" that is really a link to your PHP script. when a user requests the "directory" file from your server the PHP script is executed and it can analyze the URL.
a user can request that file like this:
www.example.com/directory
www.example.com/directory/this.html
www.example.com/directory/this/that.html
and they all exectute your php script, where it can decode the "this" and "that" portions.

That command would look like this
ln script.php directory

reptillian

7:02 pm on May 14, 2008 (gmt 0)

10+ Year Member



Hello there!

I am making my first modrewritten-URl site. So far ive used what I found in this post sucessfuly on one variable.

ive used this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/badev
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.gif$
RewriteCond %{REQUEST_URI} !\.js$
RewriteRule ^([^/]+)/? index.php?page=$1&cat=$2 [NC,QSA,L]

and the urls processed:

www.example.com/page.

Thats as far as it went for me. Please note in Rewriterule ive tried to rewrite this:

www.example.com/page/cat

to this:

www.example.com/index.php?page=somepage&cat=somecategory

But when i klick the actual link that was supposed to take me from

www.example.com/somepage to www.example.com/somepage/somecategory

i get rather: www.example.com/somecategory.

My try was:

RewriteRule ^([^/]+)/([^/]+)? index.php?page=$1&cat=$2 [NC,QSA,L]//doesnt work as desired (brakes the page)

Ive searched everywhere and tried bunch of stuff but cant seem to get the upper .htaccess code to work for more than one variable. Can someon please make an example or at least a hint would be appreciated.

best
REP

WesleyC

7:42 pm on May 14, 2008 (gmt 0)

10+ Year Member



Not entirely sure on this as I haven't worked with mod_rewrite extensively, but I have done some regex work...

RewriteRule (.+)/(.+) index.php?page=$1&cat=$2 [NC,QSA,L]

or...

RewriteRule ^([^/]+)/([^/]+)/? index.php?page=$1&cat=$2 [NC,QSA,L]

[edited by: WesleyC at 7:43 pm (utc) on May 14, 2008]