turrican_34 turrican_34 - 1 month ago 10
Apache Configuration Question

Mod Rewrite: Leading slash followed by parameter after domain name

I'm trying to create a rewrite rule that changes a URLwith parameters into just a forward slash followed by a parameter.

The RewriteRule:

RewriteRule ^(.*)$ send.php?url=$1&name=&submit=submit [NC,L]


The above rule should then work for the URL:
example.com/google.com


But although the URL
example.com/google.com
remains in the browser bar and I do not get a 404 or 500 server error, instead of adding google.com as a parameter,
send.php
is added as the URL parameter instead.

The URL should go to:
send.php?url=google.com&name=&submit=submit

But it currently goes to:
send.php?url=send.php&name=&submit=submit


Interestingly if I change the
RewriteRule
slightly, everything works fine.
Working
RewriteRule

RewriteRule ^send/(.*)$ /send.php?url=$1&name=&submit=submit [NC,L]


Here's a copy of the log for both working and not working versions of the
RewriteRule
.

Log for (working)
RewriteRule ^send/(.*)$ /send.php?url=$1&name=&submit=submit [NC,L]


[Tue Oct 11 11:03:09.500902 2016] [rewrite:trace3] [pid 6376:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:14876] 127.0.0.1 - - [example.com/sid#611988][rid#2f16c40/initial] [perdir C:/xampp/htdocs/test/] add path info postfix: C:/xampp/htdocs/test/send -> C:/xampp/htdocs/test/send/google.com

[Tue Oct 11 11:03:09.500902 2016] [rewrite:trace3] [pid 6376:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:14876] 127.0.0.1 - - [example.com/sid#611988][rid#2f16c40/initial] [perdir C:/xampp/htdocs/test/] strip per-dir prefix: C:/xampp/htdocs/test/send/google.com -> send/google.com

[Tue Oct 11 11:03:09.500902 2016] [rewrite:trace3] [pid 6376:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:14876] 127.0.0.1 - - [example.com/sid#611988][rid#2f16c40/initial] [perdir C:/xampp/htdocs/test/] applying pattern '^send/(.*)$' to uri 'send/google.com'

[Tue Oct 11 11:03:09.500902 2016] [rewrite:trace2] [pid 6376:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:14876] 127.0.0.1 - - [example.com/sid#611988][rid#2f16c40/initial] [perdir C:/xampp/htdocs/test/] rewrite 'send/google.com' -> '/send.php?url=google.com&name=&submit=submit'

[Tue Oct 11 11:03:09.500902 2016] [rewrite:trace3] [pid 6376:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:14876] 127.0.0.1 - - [example.com/sid#611988][rid#2f16c40/initial] split uri=/send.php?url=google.com&name=&submit=submit -> uri=/send.php, args=url=google.com&name=&submit=submit

Log for (not working)
RewriteRule ^(.*)$ send.php?url=$1&name=&submit=submit [NC,L]


[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] [perdir C:/xampp/htdocs/test/] strip per-dir prefix: C:/xampp/htdocs/test/google.com -> google.com

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] [perdir C:/xampp/htdocs/test/] applying pattern '^(.*)$' to uri 'google.com'

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace2] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] [perdir C:/xampp/htdocs/test/] rewrite 'google.com' -> 'send.php?url=google.com&name=&submit=submit'

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] split uri=send.php?url=google.com&name=&submit=submit -> uri=send.php, args=url=google.com&name=&submit=submit

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] [perdir C:/xampp/htdocs/test/] add per-dir prefix: send.php -> C:/xampp/htdocs/test/send.php

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace2] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] [perdir C:/xampp/htdocs/test/] strip document_root prefix: C:/xampp/htdocs/test/send.php -> /send.php

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace1] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3314c38/initial] [perdir C:/xampp/htdocs/test/] internal redirect with /send.php [INTERNAL REDIRECT]

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#331af78/initial/redir#1] [perdir C:/xampp/htdocs/test/] strip per-dir prefix: C:/xampp/htdocs/test/send.php -> send.php

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#331af78/initial/redir#1] [perdir C:/xampp/htdocs/test/] applying pattern '^(.*)$' to uri 'send.php'

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace2] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#331af78/initial/redir#1] [perdir C:/xampp/htdocs/test/] rewrite 'send.php' -> 'send.php?url=send.php&name=&submit=submit'

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#331af78/initial/redir#1] split uri=send.php?url=send.php&name=&submit=submit -> uri=send.php, args=url=send.php&name=&submit=submit

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#331af78/initial/redir#1] [perdir C:/xampp/htdocs/test/] add per-dir prefix: send.php -> C:/xampp/htdocs/test/send.php

[Tue Oct 11 11:21:58.485476 2016] [rewrite:trace1] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#331af78/initial/redir#1] [perdir C:/xampp/htdocs/test/] initial URL equal rewritten URL: C:/xampp/htdocs/test/send.php [IGNORING REWRITE]

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] add path info postfix: C:/xampp/htdocs/test/css -> C:/xampp/htdocs/test/css/style.css, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] strip per-dir prefix: C:/xampp/htdocs/test/css/style.css -> css/style.css, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] applying pattern '^(.*)$' to uri 'css/style.css', referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace2] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] rewrite 'css/style.css' -> 'send.php?url=css/style.css&name=&submit=submit', referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] split uri=send.php?url=css/style.css&name=&submit=submit -> uri=send.php, args=url=css/style.css&name=&submit=submit, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] add per-dir prefix: send.php -> C:/xampp/htdocs/test/send.php, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace2] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] strip document_root prefix: C:/xampp/htdocs/test/send.php -> /send.php, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.862783 2016] [rewrite:trace1] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3316c40/initial] [perdir C:/xampp/htdocs/test/] internal redirect with /send.php [INTERNAL REDIRECT], referer: http://example.com/google.com

[Tue Oct 11 11:22:03.863783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3319138/initial/redir#1] [perdir C:/xampp/htdocs/test/] strip per-dir prefix: C:/xampp/htdocs/test/send.php -> send.php, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.863783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3319138/initial/redir#1] [perdir C:/xampp/htdocs/test/] applying pattern '^(.*)$' to uri 'send.php', referer: http://example.com/google.com

[Tue Oct 11 11:22:03.863783 2016] [rewrite:trace2] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3319138/initial/redir#1] [perdir C:/xampp/htdocs/test/] rewrite 'send.php' -> 'send.php?url=send.php&name=&submit=submit', referer: http://example.com/google.com

[Tue Oct 11 11:22:03.863783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3319138/initial/redir#1] split uri=send.php?url=send.php&name=&submit=submit -> uri=send.php, args=url=send.php&name=&submit=submit, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.863783 2016] [rewrite:trace3] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3319138/initial/redir#1] [perdir C:/xampp/htdocs/test/] add per-dir prefix: send.php -> C:/xampp/htdocs/test/send.php, referer: http://example.com/google.com

[Tue Oct 11 11:22:03.863783 2016] [rewrite:trace1] [pid 5176:tid 1632] mod_rewrite.c(476): [client 127.0.0.1:15724] 127.0.0.1 - - [example.com/sid#3e1988][rid#3319138/initial/redir#1] [perdir C:/xampp/htdocs/test/] initial URL equal rewritten URL: C:/xampp/htdocs/test/send.php [IGNORING REWRITE], referer: http://example.com/google.com

Looking at the log it gets it correct to begin with:
url=google.com
, but then further along changes to
url=send.php


So in summary, how can I turn this URL:
example.com/send.php?url=google.com&name=&submit=submit

into this URL:
example.com/google.com

Answer

Try with:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ send.php?url=$1&name=&submit=submit [NC,L]

This way you avoid to rewrite existing files and directories.

With -f you test for files, and with -d for directories. http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond