Thứ Tư, 15 tháng 9, 2010

Rewrite url in php!


Iam using Apache. I want to rewrite url.
In url it will display: http://localhost/Ying/getrewrite/1/2.
But it redirect to: http://localhost/Ying/getrewrite.php?id=1&page=2

Step 1: Configure:

- Go /etc/httpd/conf and we configure for httpd.conf.
- Open file httpd.conf:
You find line: LoadModule rewrite_module modules/mod_rewrite.so --> delete "#" in head line to use this service.
Find line: Change AllowOverride None --> AllowOverride All.




Options FollowSymLinks
AllowOverride None



Step 2: We have 3 files and same directory:

- .htaccess (purpose: redirect virtual dir into real dir): To create this file, first open notepad, then create filename="" and extendsion: htaccess.
- rewrite.php (purpose: display link, call file getrewrite.php).
- getrewrite.php (file will be redirected).

Step 3: Content of our file:

- .htaccess:


RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^getrewrite/(.*)/(.*)$ Ying/getrewrite.php?id=$1&page=$2 [L]


Explain:
Line 1: RewriteEngine On
Line 3-4: Two lines will create requirement to server check path which user use really or not, before access it as virtual path. -d and -s are 2 sample values for checking REAL DIRECTORY and FILE. Use [OR] make it 2 in 1.
Line 5: Redirect real file. Syntax: RewriteRule Virtual_Dir Real_Dir
+ Syntax Virtual_Dir:
^ --> begin.
$ --> end.
getrewrite/(.*)/(.*) --> virtual_dir.

+ Syntax Real_Dir:
alias/filename.php?id=$1&page=$2

+ [L] --> set virtual_dir in url.

- rewrite.php:



href="getrewrite/1/2">url



- getrewrite.php:



echo "id: ".$_REQUEST["id"];
echo "page". $_REQUEST["page"];





Then now, you finish your rewrite url. Good luck!

Không có nhận xét nào:

Đăng nhận xét