开发者

mod_rewrite + "pages/123/some-text" -> "showpage.php?index=123&title=some-text"

As the title says, how can I use .htaccess and mod_rewrite to change this?

"pages/123/some-text" to "showpage.php?index=123&title=some-text"

I have started with this:

RewriteEngine On
Options +FollowSymLinks 
RewriteBase /

RewriteCond %{REQUEST_FILENAME} pages/(.*)
RewriteRule (.*) /showpage.php?index=%1 [R]

Which allows me to atleast get the parameter for index, but this fails unless I create an empty directory called pa开发者_运维百科ges.


You can just extract the relevant parts of the URL in the RewriteRule itself:

RewriteEngine On
Options +FollowSymLinks 
RewriteBase /

RewriteRule ^pages/([^/]+)/(.+)$ /showpage.php?index=$1&title=$2 [R]

Note that the R flag causes an external redirect, is this what you want? If not, you should remove it:

RewriteRule ^pages/([^/]+)/(.+)$ /showpage.php?index=$1&title=$2

Or, if you do want an external redirect, to be safe, it's a good idea to use the L flag so the redirect is applied immediately (and add the 301 status if the redirect is permanent):

RewriteRule ^pages/([^/]+)/(.+)$ /showpage.php?index=$1&title=$2 [R=301,L]

Also, if you want the title piece to be optional, you can do that too:

RewriteCond &title=$3 ^(&title=.+)$
RewriteRule ^pages/([^/]+)(/(.+))?$ /showpage.php?index=$1%1
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜