RewriteRule question

2019-08-22 17:56发布

问题:

Is there any way i can use RewriteRule to show these links:

/articles_history.php

/articles_geography.php

as

/articles/history.html

/articles/geography.html

I created a .htacesss file on my root directory and would like to know if the above is possible by placing some kind of code in the .htaccess file.

回答1:

RewriteEngine On
RewriteRule /articles/(.+)\.html /articles_$1.php [L,QSA]


回答2:

Yes it is.

See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

In addition to RewriteRule, you also need to usually turn the rewrite engine on by the RewriteEngine directive.



回答3:

Try this in your .htaccess:

RewriteEngine on
RewriteRule ^article/([^/]+)\.html$ articles_$1.php [L]

And for an arbitrary input:

RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)\.html$ $1_$2.php [L]