0

I am getting this as response,when I try an if statement to run a RewriteRule when the pages are specific. My apache server is 2.4 it is also in my screen shot. What i want to do in my code is

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f


RewriteRule ^index$ ./index.php
RewriteRule ^zindex$ ./zindex.php
RewriteRule ^logout$ ./logout.php
RewriteRule ^login$ ./login.php
RewriteRule ^zlogin$ ./zlogin.php
RewriteRule ^404$ ./404.php





#write if statement to check pages that is being loaded else fire for new username

    <If "%{HTTP_HOST} == 'index.php' || %{HTTP_HOST} == 'ajaxsignlog.php' || %{HTTP_HOST} == 'ajaxsignlog1.php' || %{HTTP_HOST} == 'ajaxupload.php' || %{HTTP_HOST} == 'connect.php' || %{HTTP_HOST} == 'logout.php' || %{HTTP_HOST} == 'password.php' || %{HTTP_HOST} == 'top.php' || %{HTTP_HOST} == 'zindex.php' || %{HTTP_HOST} == 'ztop.php'">
                     // do not rewrite rule
    </If>
    <Else> //do some rewrite rules here
    RewriteRule ^ profile.php [NC,L]
    RewriteRule ^profile/([0-9a-zA-Z]+) profile.php?id=$1 [NC,L]

    RewriteRule ^ zprofile.php [NC,L]
    RewriteRule ^zprofile/([0-9a-zA-Z]+) zprofile.php?id=$1 [NC,L]
    </Else>
Shasha
  • 439
  • 8
  • 26
  • 1
    HTTP_HOST would be domain.com not a file neam –  Nov 22 '17 at 01:10
  • @nogad thanks, but i am on localhost please could you make an example for me stuck on this please. – Shasha Nov 22 '17 at 01:11
  • "REQUEST_FILENAME The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI" https://httpd.apache.org/docs/2.4/expr.html –  Nov 22 '17 at 01:13
  • 1
    probably easer to exclude like so: `RewriteCond %{REQUEST_URI} !^/index\.php$` –  Nov 22 '17 at 01:16
  • @Timbrownlaw still getting bad request let me upload my full .htaccess code cause to be honest i am one step away from achieving semamtic url if i can get that if statement correctly just a little example. – Shasha Nov 22 '17 at 01:16
  • 1
    dupe of: https://stackoverflow.com/questions/14008003/how-to-exclude-a-specific-file-from-a-rewriterule anf a few dozen others –  Nov 22 '17 at 01:18
  • @nogad Not working does it mean If statement in .htaccess does not work. – Shasha Nov 22 '17 at 01:23
  • @Timbrownlaw Not working does it mean If statement in .htaccess does not work. – Shasha Nov 22 '17 at 01:23
  • Possible duplicate of [How to exclude a specific file from a rewriterule](https://stackoverflow.com/questions/14008003/how-to-exclude-a-specific-file-from-a-rewriterule) – Nic3500 Nov 22 '17 at 22:51

1 Answers1

1
RewriteRule ^ profile.php [NC,L]
RewriteRule ^profile/([0-9a-zA-Z]+) profile.php?id=$1 [NC,L]

The 400 Bad Request is caused by the relative path substitution inside an <If>/<Else> construct. For some reason, the directory-prefix that is added back in this context is always *If/ (regardless of the whether the directive is in the <If> or <Else> block) - which will likely result in a 400 Bad Request when performing an internal rewrite (or a 403 Forbidden if performing an external redirect).

But also, in an <If>/<Else> construct, the RewriteRule pattern matches against an absolute filesystem path, not a root-relative URL-path. So, the second directive above will never match.

So, if these restrictions/differences affect you then don't use mod_rewrite and Apache <If> expressions together. There are also other issues with how directives outside the <If> expression are also affected. (Personally, I would avoid mixing the two if at all possible.)

See also my answer to the following question on ServerFault that goes into more detail: https://serverfault.com/questions/1054363/bad-request-from-rewriterule-after-putting-if-directive

You don't need an <If>/<Else> expression to do what you are wanting. mod_rewrite itself offers the mechanism to do this, without additional tools. However, exactly how you implement this can depend on exactly what you are trying to do.

In the example in the question where you want to skip a series of rewrites if the request matches one of a series of URLs then you could do something like this:

RewriteCond %{REQUEST_URI} =/index.php [OR]
RewriteCond %{REQUEST_URI} =/ajaxsignlog.php [OR]
RewriteCond %{REQUEST_URI} =/ajaxsignlog1.php [OR]
RewriteCond %{REQUEST_URI} =/ajaxupload.php [OR]
RewriteCond %{REQUEST_URI} =/connect.php [OR]
RewriteCond %{REQUEST_URI} =/logout.php [OR]
RewriteCond %{REQUEST_URI} =/password.php [OR]
RewriteCond %{REQUEST_URI} =/top.php [OR]
RewriteCond %{REQUEST_URI} =/zindex.php [OR]
RewriteCond %{REQUEST_URI} =/ztop.php
RewriteRule ^ - [S=10]

# 10 Rules follow that are skipped when any of the above URLs are requested
# :
# :

The S=10 flag on the RewriteRule directive indicates the number of complete rules that are skipped when any of the preceding URLs are matched exactly.

See also my answer to the following question for some more ideas about ways to implement conditional branching in .htaccess: Grouping RewriteRule by RewriteCond with .htaccess

MrWhite
  • 43,179
  • 8
  • 60
  • 84