Keyword rich and human readable url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Keyword rich and human readable url

Post by kkonline »

Hello there,
I read scottayy's snippet on viewtopic.php?p=317399#317399

and tried to use it with my article manager having current url structure

http://localhost/dir/pages.php?sid=1&ca ... e=2&mode=1

i used

Code: Select all

RewriteEngine On
RewriteRule ^/pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/$ /pages.php?sid=$1&catid=$0&page=$1&mode=$1
but when i write http://localhost/dir/pages/1/0/2/1 it should load the contents of page http://localhost/dir/pages.php?sid=1&ca ... e=2&mode=1

Note: mode=1 or 2 or... whatever is optional for displaying the comments and writing comments

but that is not happening?

Scottayy please guide me how to rewrite the above rule in order to get the desired url
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

According to you rewrite rule, all the numbers after '/pages/' will become the parameter value. So

/pages/123/456/

Will become:

/pages.php?page=123
(#10850)
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

arborint wrote:According to you rewrite rule, all the numbers after '/pages/' will become the parameter value. So

/pages/123/456/

Will become:

/pages.php?page=123
I think i wrote something wrong i want
http://localhost/dir/pages/1/0/2/1 to show the contents of page http://localhost/dir/pages.php?sid=1&ca ... e=2&mode=1

then how to do this?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You don't need my class to do this, as my class deals with the formatting of strings to strip invalid characters from a URL.

All you need is your .htaccess rule

Code: Select all

RewriteEngine On 
RewriteRule ^/pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/$ /pages.php?sid=$1&catid=$2&page=$3&mode=$4
If you wanted to use my class, it'd be ideal for if you wanted words in your url. Such as /dir/pages/1/category-name/2/page-name/3/mode-name
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

Scottayy, I used the rule you specified.

Code: Select all

RewriteEngine On
RewriteRule ^/pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/$ /pages.php?sid=$1&catid=$2&page=$3&mode=$4
when i write http://localhost/pages/1/0/2/1 on address bar i get 404 Not Found error

Secondly I would like the pages to become like http://localhost/pages/1/0/2/1/The_title_of_page
using your snippet so what to write in modrewrite rules

and currently in pages.php ur snippet execution is like

Code: Select all

$safeurl = new safeurl();
$safeurl->lowercase = false;
$safeurl->whole_word = false; 
echo '<a href="/1/0/1/'.$safeurl->make_safe_url($title).'">'.$title.'</a>';
How to do it?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

kkonline wrote:Scottayy, I used the rule you specified.

Code: Select all

RewriteEngine On
RewriteRule ^/pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/$ /pages.php?sid=$1&catid=$2&page=$3&mode=$4
when i write http://localhost/pages/1/0/2/1 on address bar i get 404 Not Found error

Code: Select all

RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)(/*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
kkonline wrote:Secondly I would like the pages to become like http://localhost/pages/1/0/2/1/The_title_of_page

Code: Select all

RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

anjanesh wrote:

Code: Select all

RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
http://localhost/pages/1/0/1/1/about-success
Still gives me 404 Not Found

However if i write

http://localhost/pages.php/1/0/1/1/about-success

Then the first page is displayed by writing

http://localhost/pages.php/1/0/1/1/

But i am not able to access the second page by writing http://localhost/pages.php/1/0/2/1/some-text

The contents of first page (page=1) are only displayed

What to do?
Last edited by kkonline on Wed Sep 05, 2007 12:55 am, edited 3 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

echo '<pre>';
print_r($_GET);
echo '</pre>';
Run that on your page and see what it returns for you. See if the page is correct. In your mod rewrite rule, the first set of () is captured by $1 and the second set is captured by $2.. etc.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

To access http://localhost/pages/1/0/2/1 ,

Code: Select all

RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)(/*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
should work.
But http://localhost/pages/1/0/2/1/The_title_of_page would return 404.
For http://localhost/pages/1/0/2/1/The_title_of_page ,

Code: Select all

RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
should work though.

But it seems you want both to work ?
Just combine them

Code: Select all

RewriteEngine On
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)(/*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
for these to work
http://localhost/pages/1/0/2/1
http://localhost/pages/1/0/2/1/
http://localhost/pages/1/0/2/1/The_title_of_page
http://localhost/pages/1/0/2/1/The_title_of_page/
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

scottayy wrote:

Code: Select all

echo '<pre>';
print_r($_GET);
echo '</pre>';
It just prints

Code: Select all

Array
(
)


to whatever page i try to access and just displays the first page.

In .htaccess i wrote

Code: Select all

Order deny,allow
allow from 127.0.0.1
deny from all

RewriteEngine On
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
Then on the browser i wrote http://localhost/pages.php/1/0/1/about-some-text

then the contents of page 1 are displayed, then if i write http://localhost/pages.php/1/0/3/1/about-some-text
i still get the contents of page 1 (synonymous to localhost/pages.php?page=1)

How to access the second page or the third... ?

If i write http://localhost/pages/1/0/1/0/about-su ... d-kkonline i get 404 not found


However in beginning of the pages.php i am using

Code: Select all

if(!isset($_GET['page'])){
    $page = 1;
}else{
    if(ctype_digit($_GET['page'])){
        $page=trim(mysql_real_escape_string($_GET['page']));
    }else{
        echo "invalid query";
        exit;
    }
}
and similar for mode, catid and sid should i remove this part?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

kkonline wrote:In .htaccess i wrote

Code: Select all

Order deny,allow
allow from 127.0.0.1
deny from all

RewriteEngine On
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
Then on the browser i wrote http://localhost/pages.php/1/0/1/about-some-text

then the contents of page 1 are displayed, then if i write http://localhost/pages.php/1/0/3/1/about-some-text
i still get the contents of page 1 (synonymous to localhost/pages.php?page=1)
Odd, your url should've been http://localhost/pages/1/0/1/2/about-some-text and not http://localhost/pages.php/1/0/1/about-some-text
kkonline wrote:How to access the second page or the third... ?

If i write http://localhost/pages/1/0/1/0/about-su ... d-kkonline i get 404 not found
You sure about this ?
Try creating a pages.php in your localhost folder.

Code: Select all

<pre><?php
print_r($_GET);
?>
And in your .htaccess file

Code: Select all

RewriteEngine On
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)(/*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
http://localhost/pages/1/0/1/0/about-su ... d-kkonline should return

Code: Select all

Array
(
    [sid] => 1
    [catid] => 0
    [page] => 1
    [mode] => 0
)
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

previously mod_rewrite module was not enabled!

I Uncommented

LoadModule rewrite_module modules/mod_rewrite.so
and
AddModule mod_rewrite.c
in httpd.conf file, then restarted apache but still http://localhost/pages/1/0/1/0/about...s-and-kkonline gives me 404 not found!

There is no mention of mod_rewrite in phpinfo(); how do i confirm if mod_rewrite is enabled. Because i suspect it's still not enabled or loaded or something!

I wrote

Code: Select all

Order deny,allow
allow from 127.0.0.1
deny from all

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)(/*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
</IfModule>
in .htaccess and then http://localhost/pages/1/0/1/0/ still gives me 404 Not found
Last edited by kkonline on Wed Sep 05, 2007 1:36 am, edited 1 time in total.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

You sure you have

Code: Select all

AllowOverride All
AccessFileName .htaccess
in your httpd.conf file ?
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

anjanesh wrote:You sure you have

Code: Select all

AllowOverride All
AccessFileName .htaccess
in your httpd.conf file ?
httpd.conf had only

AccessFileName .htaccess written somewhere
i replaced it with

AllowOverride All
AccessFileName .htaccess
Then it gives the error Syntax error on line 405 :AllowOverride not allowed here

I then turned it back to the original thing...

and wrote .htaccess as below

Code: Select all

Order deny,allow
allow from 127.0.0.1
deny from all

<IfModule mod_rewrite.c>
Options +FollowSymLinks
AllowOverride All

RewriteEngine On
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)(/*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
</IfModule>
No luck yet!
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Let AccessFileName .htaccess be where it was.

Assuming your htdocs is located at C:\Program Files\Apache Group\Apache2\htdocs :

Somewhere you should see something like <Directory "C:/Program Files/Apache Group/Apache2/htdocs"> - its within that block segment you should add/modify to AllowOverride All.

Code: Select all

<Directory "C:/Program Files/Apache Group/Apache2/htdocs">

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All

</Directory>
Post Reply