开发者

How to catch all characters with slashes after domain address with mod_rewrite as one parameter?

How to catch all characters after domain name as one parameter with mod_rewrite? I have script which load other page content and displays that like standard page. example script address look like that:

example.com/script.php/?www='/some/paths/etc'
开发者_开发问答

I want to mask that URL to look like this:

example.com/some/paths/etc

If no paths exist it should look like

example.com/

There can be different amount of URL parts (a/b/c/.../X/)

I red about mod_proxy but I can do it with only .htaccess so it doesn't solve my problem. I can't redirect one address to other, they have to be separated.


You can proxy via mod_rewrite (as long as mod_proxy has been compiled into the server), and this can be done via .htaccess files. Note that RewriteEngine On requires Options FollowSymLinks to be overridable in your .htaccess.

The following in your .htaccess file:

 Options FollowSymLinks
 RewriteEngine On
 RewriteCond %{REQUEST_URI} ^/script.php/?www=
 RewriteRule ^/script.php/?www='([^']+)' http://example.com/$1
 RewriteRule ^/(.*) /script.php/?www='$1' [P]

The RewriteCond matches your script path with a ?www= argument (if it fails to match no rewrite will take place). The first RewriteRule will match the www= argument and rewrite the visible path of the request. The second RewriteRule will then proxy the request (due to the [P] flag) to the script and will get the "content".

Note: this has not been tested, but hopefully will get you heading in the right direction to solve the problem. Also if script.php expects optional args &var2=whatever you will probably need to tweak the RewriteCond to match that and store it using parentheses. You can then recall those args using %1. More details can be found in the documentation.

Also to help you debug your rewriting you can use:

RewriteLog /full/path/to/your/log/file
RewriteLogLevel 3

EDIT: Added the RewriteLog stuff an clarified that the snippet at the top is to be put in the .htaccess file

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜