使用wamp和php实现URL重写

I can't implement URL rewriting.

What I've done so far:

1. Enabled rewrite_module in apache.

2. Added a .htaccess file to my website (test) in the directory:

127.0.0.1/test/

which contains the following:

RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

3. I have a news.php file which contains

echo "hello world";

Now, whenever I go to URL: http://127.0.0.1/news/1, I get a

'Page not found'

I was successful in the following.

Files:

document_root/
|- .htaccess
|- news.php

.htaccess:

RewriteEngine on
RewriteRule news/([0-9]+)\.html news\.php?news_id=$1

http://127.0.0.1/news/1.html
-> hello world

I suppose that you've successfully enabled the rewrite module (rewrite_module) of your Apache server, and that you've put your .htaccess file inside the test directory which is in the root directory of your web server ( like for example : c:\wamp\www\test).

So here we have 2 cases of what (I suppose) you want to do :

    1. Access to your URL using, for example : http://127.0.0.1/test/news/1.html.
    1. Access to your URL using, for example : http://127.0.0.1/news/1.html.

So, for the 1st case, you have just to edit your .htaccess file like this :

RewriteEngine on
RewriteRule ^news/([0-9]+)\.html news.php?news_id=$1

Here any URL like http://127.0.0.1/test/news/1.html will be rewriting to test ews.php and pass to it the news_id param.


And for the 2nd case, you have to put your .htaccess file into the web server root directory and add just the RewriteBase directive

The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.

so your .htaccess will be like this :

RewriteEngine on
RewriteBase /test
RewriteRule ^news/([0-9]+)\.html news.php?news_id=$1

Of course you can find many many tutorials on the net about how to rewrite toyr URLs using the .htaccess file ...

Hope that can help.