0

My Apache server will be receiving incoming requests from users in this format: http://192.168.1.182/SEARCH=searchstring&URL=http://test.com/blahblahblah

I want to redirect users to another domain, but keep the searchstring and add that at the end of the new URL.

Example: https://newdomain.com/#q=searchstring

What is the best way to do this?

I read the Apache Docs on Redirecting and Remapping with mod_rewrite and tried various examples but couldn't get anything to work.

Froggiz
  • 3,043
  • 1
  • 19
  • 30

1 Answers1

0

Simply like this (if your URL param is always after the SEARCH param)

RewriteEngine On
RewriteCond %{QUERY_STRING} SEARCH=(.*?)&URL= [NC]
RewriteRule . https://newdomain.com/#q=%1 [R=301,L]

I added flag NC for non-case sensitive, like that it work with SEARCH= and search=

If your URL parameters may not exist:

RewriteEngine On
RewriteCond %{QUERY_STRING} SEARCH=([^&]*) [NC]
RewriteRule .* https://newdomain.com/#q=%1 [R=301,L]

To enable .htaccess, you have to add AllowOverride All to your web folder, and enable mod_rewrite (terminal command : a2enmod rewrite)

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Froggiz
  • 3,043
  • 1
  • 19
  • 30
  • Thanks. Do i need to add anything else to my .htaccess or that's it? – 16b7195abb140a3929bbc322d1c6f1 Dec 08 '15 at 13:40
  • Only `RewriteEngine On` before to allow Rewrite – Froggiz Dec 08 '15 at 13:41
  • Thanks for your help, but no luck. Here's what appears in the URL bar on the user's browser:`http://192.168.1.182/SEARCH=searchstringURL=https%3A//www.site.com/blahblahblah`It appears they aren't getting re-directed at all. – 16b7195abb140a3929bbc322d1c6f1 Dec 08 '15 at 13:59
  • your url is incorrect it miss & before URL, and url in it need to be URL encoded – Froggiz Dec 08 '15 at 14:06
  • Is there a way to URL encode everything that comes in, then apply the RewriteRule? Also, apologies but it's definitely the .htaccess file in the /var/www/html root isn't it? – 16b7195abb140a3929bbc322d1c6f1 Dec 08 '15 at 14:15
  • If /var/www/ is your root folder it has to be in /var/www/.htaccess else If /var/www/html/ is your root folder it has to be in /var/www/html/.htaccess. To url encode you have to use your server language like hp if you are using php to encode you url before redirecting or writing the url in your page. First try `http://192.168.1.182/SEARCH=searchstring` and then `http://192.168.1.182/SEARCH=searchstring&URL=https%3A//www.site.com/blah‌​` in your browser – Froggiz Dec 08 '15 at 14:24
  • Thanks! Got it working! I had to fix my `apache2.conf` and add ` Options Indexes FollowSymLinks AllowOverride All Require all granted ` then i had to run this command `sudo a2enmod rewrite && sudo service apache2 restart` – 16b7195abb140a3929bbc322d1c6f1 Dec 09 '15 at 02:53
  • You will also need the `NE` flag on the `RewriteRule` if you are redirecting to a URL with a fragment identifier (ie. `#`), otherwise this will be URL encoded and seen as part of the URL-path. Also, include the `QSD` flag (Apache 2.4+) in order to discard the query string from the original request, otherwise this will be passed through and appended to the end of the target URL - as part of the fragment identifier! – MrWhite Nov 17 '16 at 14:27