Home > database >  htaccess rewrite if url doesnt have a query string
htaccess rewrite if url doesnt have a query string

Time:06-14

I have a htaccess that rewrites to /login if conditions aren't met. It's working fine for urls without query_string.

However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset

/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj

RewriteEngine On
RewriteBase /index.php

RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$

RewriteRule ^([^\.] )$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.] )$ $1.php [NC,L]

CodePudding user response:

Here is a modified version of your .htaccess that excludes /reset?query:

RewriteEngine On

# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]

RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.] )$ $1.php [L]

Make sure to test it after clearing your browser cache.

CodePudding user response:

With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.

Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.

RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$ 
RewriteRule ^reset/?$ /login? [R=301,L,NC]

##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S \s [NC]
RewriteRule ^ /reset? [R=301,NC]

##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]

##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]

##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
  • Related