skip to Main Content

I am making some changes in .htacess file and i want to capture the requested url, create substring of it and pass it to third part url.
For example

I am working on ubuntu, so currently my .htaccess file is in folder test. So when i access localhost/test/http://www.facebook.com , it takes me to service.prerender.io/test/http://www.facebook.com but i want to eliminate the ‘test’ and forwrd the request like service.prerender.io/http://www.facebook.com

Here is the code of my .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/script.php
RewriteRule ^(.*)$ http://service.prerender.io%{REQUEST_URI} [L,QSA]
</IfModule>

2

Answers


  1. You can use the RewriteCond %{REQUEST_URI} to set capture groups and instead of rewriting to http://service.prerender.io%{REQUEST_URI} you can rewrite to something like this http://service.prerender.io/%1.

    Here is an example (untested):

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/script.php
    RewriteCond %{REQUEST_URI} ^/?test/([.]+) [NC]
    RewriteRule ^(.*)$ http://service.prerender.io/%1? [L,QSA]
    </IfModule>
    
    Login or Signup to reply.
  2. What you are trying to captures is not directly the requested uri but part of it. You can get it from rewrite rule

    Try following

    RewriteRule ^test/(.*)$ http://service.prerender.io/$1? [L,QSA]
    

    If you’re accessing a page something like,

    https://www.example.com/thisdir/thisfile.php
    

    Then you will get following as request uri and script uri

    REQUEST_URI: /thisdir/thisfile.php
    
    SCRIPT_URI: https://www.example.com/thisdir/thisfile.php
    

    And https://github.com/prerender/prerender-apache is more information about htaccess and prerender

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search