0

This is my first time trying to write htaccess rules. My target is to make all relative links to be redirected to absolute. To begin i was testing:

RewriteEngine On

RewriteRule gallery\.php$ http://www.domain.com/sample/gallery.php
RewriteRule info\.php$ http://www.domain.com/sample/gallery.php

The first rule makes Firefox throw the error "The page isn't redirecting properly" when I click on the link, while the second rule works fine. The idea for the future was to write a rule like,

RewriteRule catchAllRelativeLinks$ http: //www.domain.com/sample/$1

but if I can't make the first rule work I don't think I will find how to make the real rule.

EDIT: to avoid the endless loop can't i try to understand if i am on the first or second istance by catching a variable ? some ideas i tried (and faild):

RewriteCond %{IS_SUBREQ} false
RewriteRule ^gallery\.php$ http://www.domain.com/gallery.php?a [R=302,L]

RewriteCond %{THE_REQUEST} !(\?something)$
RewriteRule ^gallery\.php$ http://www.domain.com/gallery.php?something [R=302,L]

or with an environment variable,

thanks again

Riccardo
  • 346
  • 2
  • 17
  • What is the first intended to match? is it meant for `/gallery.php` to redirect into `/sample/gallery.php`? If so, it should include `^` as in `^gallery\.php$` – Michael Berkowski Nov 09 '12 at 17:37
  • U can checkout this explanation and guide for the same (: http://stackoverflow.com/a/21754308/5465790 – Walk Apr 06 '17 at 10:26

2 Answers2

1

The first rule you have results in an endless loop. mod_rewrite isn't all that great at making relative links absolute. That should be done in the HTML. For example, turning

<a href="gallery.php">Gallery</a>

into

<a href="/sample/gallery.php">Gallery</a>

However, the second rule works because it does something mod_rewrite does extremely well, and that's redirecting from one page or URL to another. For more information on this please see, the Apache documentation.

quentinxs
  • 866
  • 8
  • 22
0

You can checkout this link for detailed explanation.

https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

This worked for me (:


    RewriteEngine on

    # Check for POST Submission | 
    # Because POST parameters aren't retained on a redirect.
    # You can omit that line if you want to make sure that all POST submissions are secure   
    # (any unsecured POST submissions will be ignored)
    RewriteCond %{REQUEST_METHOD} !^POST$

    # Forcing HTTPS
    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{SERVER_PORT} 80
    # Pages to Apply
    RewriteCond %{REQUEST_URI} ^something_secure [OR]
    RewriteCond %{REQUEST_URI} ^something_else_secure
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Walk
  • 1,531
  • 17
  • 21