1

I have created .htaccess file like this, here I am searching for DB string, if uri contains DB string then write complete uri to variable url and redirect, so far I tried this.. I don't know much about mod_rewrite and htaccess.

Options +FollowSymlinks
RewriteEngine on

# If URI contains string DB then encode http_host and request uri and 
# send to http://host.domain.org/site2/index.php with new variable url=encodedstring

RewriteCond %{REQUEST_URI} /DB/
RewriteRule ^ http://host.domain.org/site2/index.php?url=%{HTTP_HOST}%{REQUEST_URI} [NE]

Suppose if my url is

  http://host.domain.org/site2/DB/process.php?name=x&y=10

I would like url variable to be this

 $ echo 'http://host.domain.org/site2/DB/process.php?name=x&y=10' | base64 
 aHR0cDovL2hvc3QuZG9tYWluLm9yZy9zaXRlMi9EQi9wcm9jZXNzLnBocD9uYW1lPXgmeT0xMAo=

I want url should be like this

 http://host.domain.org/site2/index.php?url=aHR0cDovL2hvc3QuZG9tYWluLm9yZy9zaXRlMi9EQi9wcm9jZXNzLnBocD9uYW1lPXgmeT0xMAo=

I don't know this is possible or not in .htaccess kindly someone help me

I even tried to replace all slashes to hypen like this at last

slash.sh
#!/usr/bin/env bash
sed -u 's/\//-/g'

.htaccess file

RewriteCond %{REQUEST_URI} /DB/
RewriteMap slash prg:/var/www/virtual/slash.sh
http://host.domain.org/site2/index.php?url="${slash:%{HTTP_HOST}}${slash:%{REQUEST_URI}}" [L]

Suppose if my url is

  http://host.domain.org/site2/DB/process.php?name=x&y=10

I want url should be like this

 http://host.domain.org/site2/index.php?url="http:--host.domain.org-site2-DB-process.php?name=x&y=10"

I received an error 500 Internal Server error

So finally neither base64 nor slash replace worked for me.. its my hard luck

Thanks.

user3637224
  • 585
  • 1
  • 3
  • 22

1 Answers1

1

These rules are tricky but since your requirement is as such I am providing you an answer here.

Please this in DocumentRoot/.htaccess:

RewriteEngine On

RewriteRule ^site2/DB/process\.php$ /site2/index.php/http://%{HTTP_HOST}%{REQUEST_URI} [NC,NE,L]

RewriteCond %{REQUEST_URI} ^(/site2/index.php)/([^/]*)/([^/]*/.*)$
RewriteRule ^ %1/%2-%3 [L,NE]

RewriteCond %{REQUEST_URI} ^(/site2/index.php)/([^/]*)/([^/]*)$ 
RewriteRule ^ %1?url=%2-%3 [L,NE,QSA,R=302]

This will redirect a URI like this: /site2/DB/process.php?name=x&y=10

to this: /site2/index.php?url=http:--www.localhost-site2-DB-process.php&name=x&y=10

Thus replacing all the hyphens by - in the url query parameter.

anubhava
  • 761,203
  • 64
  • 569
  • 643