Page 1 of 2
Keyword rich and human readable url
Posted: Tue Sep 04, 2007 11:33 pm
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
Posted: Wed Sep 05, 2007 12:04 am
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
Posted: Wed Sep 05, 2007 12:14 am
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?
Posted: Wed Sep 05, 2007 12:19 am
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
Posted: Wed Sep 05, 2007 12:29 am
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?
Posted: Wed Sep 05, 2007 12:40 am
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
Code: Select all
RewriteRule ^pages/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ pages.php?sid=$1&catid=$2&page=$3&mode=$4
Posted: Wed Sep 05, 2007 12:46 am
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?
Posted: Wed Sep 05, 2007 12:51 am
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.
Posted: Wed Sep 05, 2007 12:57 am
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/
Posted: Wed Sep 05, 2007 1:01 am
by kkonline
scottayy wrote:Code: Select all
echo '<pre>';
print_r($_GET);
echo '</pre>';
It just prints
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?
Posted: Wed Sep 05, 2007 1:19 am
by anjanesh
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
You sure about this ?
Try creating a pages.php in your localhost folder.
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
)
Posted: Wed Sep 05, 2007 1:26 am
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
Posted: Wed Sep 05, 2007 1:35 am
by anjanesh
You sure you have
Code: Select all
AllowOverride All
AccessFileName .htaccess
in your httpd.conf file ?
Posted: Wed Sep 05, 2007 1:46 am
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!
Posted: Wed Sep 05, 2007 1:54 am
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>