mod rewrite beginner questions

落爺英雄遲暮 提交于 2020-01-14 04:42:32

问题


How would I rewrite script.php?id=3295 to script/3295??

and Im also wondering if someone could explain what these 3 RewriteConds does:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

Thank you very much


回答1:


As I rather think that you want to rewrite requests of /script/3295 to /script.php?id=3295 and not the other way round, try this:

RewriteRule ^/script/([0-9]+)$ /script.php?id=$1

But if you really want to rewrite script.php?id=3295 to script/3295, try this:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
RewriteRule ^/script\.php$ /script/%3?%1%4

And if you want to use that rule in a .htaccess file, remove the leading slash from the pattern.

To your second question: The RewriteCond directives test if the requested URI cannot be mapped to an existing regular file (!-f), not to an existing directory (!-d) and not to an existing symbolic link (!-l).




回答2:


RewriteRule ^/sctipt/(.*) /script.php?id=$1



回答3:


For your first question, try:

RewriteRule ^script.php?id=(.*)$ /script/$1

For your second question, RewriteCond are conditions applied to the next RewriteRule. So if you see three RewriteCond like that all in a row, they all apply to only the next rule. The rule is only checked if all of those conditions match. In this particular case, it is checking the requested filename to make sure that it is not a file (!-f), not a directory (!-d) and not a symbolic link (!-l). If all of these conditions are true, mod_rewrite will process the next RewriteRule.



来源:https://stackoverflow.com/questions/1249368/mod-rewrite-beginner-questions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!