skip to Main Content

redirect url exact match with htaccess not redirect if query string or anything after index.php

Redirect This Page: https://example.com/demo/index.php
To Page: https://example.com/ (home page)

But Do Not redirect: https://example.com/demo/index.php/*

 Do NOT redirect: https://example.com/demo/index.php/password

 Do NOT redirect https://example.com/demo/index.php?m=page
  not redirect if any other combination 

only redirect https://example.com/demo/index.php to https://example.com/

this script not working

RewriteEngine On
RewriteBase /
RewriteRule ^demo/index.php /demo/index.php?m=page[L,NC,END]
RewriteRule ^demo/index.php$ https://example.com/ [L,R=301]

3

Answers


  1. only redirect https://example.com/demo/index.php to https://example.com/

    If you only want to redirect that exact URL, without a query string then you need to reverse your existing rules and include an additional condition that checks that the QUERY_STRING is empty. The RewriteRule only matches against the URL-path, which excludes the query string.

    You are also missing a space before the flags argument in the first rule.

    For example:

    # Redirect exact URL to home page
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^demo/index.php$ / [R=302,L]
    
    # Redirect other URLs with query string OR path-info
    RewriteRule ^demo/index.php /demo/index.php?m=page [NC,L]
    

    You don’t need L and END.

    Test with 302, and change to 301 only once you have confirmed it works OK. To avoid caching issues.

    Login or Signup to reply.
  2. You may use these rules in site root .htaccess:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} s/+demo/index.phps [NC]
    RewriteRule ^ / [L,R=301]
    
    RewriteRule ^demo/index.php$ $0?m=page [QSA,NC,L]
    
    Login or Signup to reply.
  3. This was created by htaccess redirect generator I use very often.

    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^demo/index.php$ https://example.com/? [R=301,L]
    

    Maybe “?” sign is redundant because of empty query string match.

    Not sure, but You also can try to add

    RewriteCond %{REQUEST_METHOD} !=POST 
    

    in case You can’t log in. All together:

    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^demo/index.php$ https://example.com/? [R=301,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search