开发者

Using .htaccess to rewrite clean & custom URL structure

开发者 https://www.devze.com 2023-01-06 19:39 出处:网络
How do i redirect website.com/about.php to website.com/about Also, is it possible to manually create the app开发者_JAVA百科earance of subdirectories using .htaccess?

How do i redirect

website.com/about.php

to

website.com/about


Also, is it possible to manually create the app开发者_JAVA百科earance of subdirectories using .htaccess?

i.e. website.com/project1.php

to

website.com/projects/project1

Much appreciated!


Yes!

You can use the Apache RewriteEngine module to achieve this end. The documentation is here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

There are a number of good tutorials around also.


Some examples of rules are:

RewriteRule   ^/(\S+).php /$1

RewriteRule   ^/(project\d+).php /projects/$1

However, these are just guesses at what you might want to achieve, there are always corner cases. (Also, these are not tested!)

Bear in mind the above two rules would not necessarily be good together as written. Take note in the documentation about setting up the RewriteEngine and using appropriate RewriteRule options.

For example, when specifying many RewriteRules it is common to specify the [L] option so that the RewriteEngine stops rewriting after the rule is applied. Ordering of rules, therefore, can be significant.


Here are your examples putting the .htaccess on your root directory and using mod_rewrite

RewriteEngine on

RewriteRule ^about\.php$ about [R]

RewriteRule ^projects/project([0-9]+)$ project$1.php
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号