简体   繁体   English

.htacess中的重写URL在PHP中不起作用

[英]rewrite url in .htacess not working in php

I am working using php and apache server, I want to rewrite sites/all/themes/chap2014/fpd/gift.php to fdp/gift and sites/all/themes/chap2014/fpd/another.php fdp/another exactly I want do this 我正在使用php和apache服务器工作,我想将sites/all/themes/chap2014/fpd/gift.phpfdp/giftsites/all/themes/chap2014/fpd/another.php fdp/another我想要的fdp/another做这个

rewrite sites/all/themes/chap2014/fpd/*.php to fpd/* sites/all/themes/chap2014/fpd/*.php fpd/*fpd/*

rewrite mode is enable and I try below code in .htacess file. 重写模式已启用,我尝试在.htacess文件中使用以下代码。

RewriteEngine on
RewriteRule ^fpd/?$  sites/all/themes/chap2014/fpd/$1.php

but nothing happened,what is the correct way to achieve this ? 但是什么也没发生,实现这一目标的正确方法是什么?

Appreciate any help, 感谢任何帮助,

You need to first group and capture part of URI on LHS of URI pattern before you can use $1 , $2 etc on RHS. 您需要先在URI模式的LHS上分组并捕获URI的一部分,然后才能在RHS上使用$1$2等。

RewriteEngine on

RewriteRule ^fpd/([^/]+)/?$ /sites/all/themes/chap2014/fpd/$1.php [L,NC]

PS: In your question you have used both fpd and fdp . PS:在您的问题中,您同时使用了fpdfdp Not sure which one is right. 不知道哪一个是正确的。

Try this: 尝试这个:

RewriteRule ^fpd/(.+)$ sites/all/themes/chap2014/fpd/$1.php

You might want to exclude the / symbol, if so, then replace the .+ with [^/]+ . 您可能希望排除/符号,如果是的话,则将.+替换为[^/]+

You have to enclose the part that you want placed into a variable with () , and each enclosed part will be put into $1, $2, $3 and so on. 您必须使用()将要放入变量的部分括起来,每个被包围的部分将放入$ 1,$ 2,$ 3等。

Also, here is a nice tool to test your rewrite rules: 另外,这是一个测试重写规则的好工具:

http://martinmelin.se/rewrite-rule-tester/ http://martinmelin.se/rewrite-rule-tester/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM