简体   繁体   English

PHP APACHE URL重写

[英]PHP APACHE URL rewriting

I have been searching all over the web for a solid 2 hours now trying different things, and none of what i found would work. 我一直在网上搜索一个完整的2个小时,现在尝试进行其他操作,但发现没有任何效果。

I want to redirect to a non-existent folder and then convert that into a query-string - for example an anchor tag that leads to "domain.com/test/" But .htaccess should intercept that and change it into "domain.com/index.php?p=test" Behind the curtains without the viewer seeing the query-string 我想重定向到一个不存在的文件夹,然后将其转换为查询字符串-例如,一个指向“ domain.com/test/”的锚标记,但是.htaccess应该拦截它并将其更改为“ domain.com” /index.php?p=test“在幕后,而查看者看不到查询字符串

I am not sure what I am doing wrong, but I have tried pretty much everything on google - page 1 and two at least :P 我不确定自己在做什么错,但是我已经尝试了Google上的几乎所有内容-第1页和至少第2页:P

Any help is greatly appreciated. 任何帮助是极大的赞赏。

mod_rewrite has attribute tests that allows you to test if the requested url is a file or a directory. mod_rewrite具有属性测试,可让您测试所请求的URL是文件还是目录。 See the documentation . 请参阅文档

To use this, you use a RewriteCond to test %{REQUEST_FILENAME} . 要使用此功能,请使用RewriteCond测试%{REQUEST_FILENAME} You can then redirect or internally rewrite the url. 然后,您可以重定向或内部重写该URL。 A redirect will change the url visibly for the client, while an internal redirect will only show a different page without actually changing the url. 重定向将明显更改客户端的url,而内部重定向将仅显示其他页面,而无需实际更改url。

#The requested url is NOT a directory AND NOT a file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [L]

If you want an external redirect to happen, add the R -flag. 如果要进行外部重定向,请添加R标志。 The R=301 flag will make the redirect permanent, but only make it permanent if all rules work as you expect them to work. R=301标志将使重定向永久化,但仅当所有规则均按您期望的那样起作用时,才将其永久化。

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

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