Avatar of Neil Thompson
Neil Thompson
Flag for United Kingdom of Great Britain and Northern Ireland asked on

rewrite in htaccess only following the last rewrite rule, ignoring the others

hi all

I have an issue with my rewrite in a .htaccess file that I'm unsure how to fix.

I want to be able to pass parameters as a query variable when someone uses the url for example www.mysite.com/user/reset/asdfg12345 to be re-written as www.mysite.com/user/reset/?rp=asdfg12345

My code below works but only for the very last rule so in this case RewriteRule ^user/verify/([^/\.]+)/?$      user/verify/?uvc=$1 [L] , the rule or rules above it never work, If I change the order again the last one only ever works.

Sure it's a simple error but something I'm not sure about.

Thanks in advance all, Neil


RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$  

# allows reset URL like /user/reset/5d34f4e59c155566913f782a2e2557dc,234j5hj23k452hk
RewriteRule ^user/reset/([^/\.]+)/?$	user/reset/?rp=$1 [L] 

# allows verification URL like /user/verify/5d34f4e59c155566913f782a2e2557dc
RewriteRule ^user/verify/([^/\.]+)/?$	user/verify/?uvc=$1 [L]

ErrorDocument 404 404.php 

############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

## rewrite everything else to index.php
RewriteRule .* index.php [L]

Open in new window

PHPApache Web Server* URL Rewrite Rules

Avatar of undefined
Last Comment
Neil Thompson

8/22/2022 - Mon
Dr. Klahn

Apache applies mod_rewrite rules in order they are encountered, and normally makes only one pass through.

Unless it is 100% sure that no following rewrite rules could ever be applied to a rewritten URL, the [L] flag should not be applied.  This terminates rewrite processing and sends the URL to Apache for production.  [L] means that (on a match) no following rules can ever be applied.

If a rewrite results in a URL that might require further rewriting by rules previously encountered, [R=302] should be applied so that the request comes back to Apache and it can have another go at it.
Neil Thompson

ASKER
Even removing the [L] and adding the r=302 it's still only the last one of these that is ever actually actioned

In this instance only a URL like /user/verify/5d34f4e59c155566913f782a2e2557dc will work unless, again altering the location to top this one fails and the /user/reset/5d34f4e59c155566913f782a2e2557dc,234j5hj23k452hk will work.

BOTH work if I simply append the query variable, e.g. ?rp= or ?uvc=

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$  

# allows reset URL like /user/reset/5d34f4e59c155566913f782a2e2557dc,234j5hj23k452hk
RewriteRule ^user/reset/([^/\.]+)/?$	user/reset/?rp=$1

# allows verification URL like /user/verify/5d34f4e59c155566913f782a2e2557dc
RewriteRule ^user/verify/([^/\.]+)/?$	user/verify/?uvc=$1

ErrorDocument 404 404.php 

Open in new window

ASKER CERTIFIED SOLUTION
Dr. Klahn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Neil Thompson

ASKER
Many thanks, below is the log for an attempt for both, verify failing and falling to 404.php

[Wed Jan 23 20:02:17.796472 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56417] AH01626: authorization result of Require all granted: granted
[Wed Jan 23 20:02:17.796472 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56417] AH01626: authorization result of <RequireAny>: granted
[Wed Jan 23 20:02:17.796472 2019] [core:trace3] [pid 13168:tid 1312] request.c(302): [client ::1:56417] request authorized without authentication by access_checker_ex hook: /user/verify/35435345
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/35435345 -> user/verify/35435345
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/reset/([^/\\.]+)/?$' to uri 'user/verify/35435345'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/35435345 -> user/verify/35435345
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/verify/([^/\\.]+)/?$' to uri 'user/verify/35435345'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace2] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] rewrite 'user/verify/35435345' -> 'user/verify/?uvc=35435345'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] split uri=user/verify/?uvc=35435345 -> uri=user/verify/, args=uvc=35435345
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] add per-dir prefix: user/verify/ -> D:/htdocs/SelfBuildRegister/user/verify/
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace2] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] trying to replace prefix D:/htdocs/SelfBuildRegister/ with /
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace1] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503dc2c0/initial] [perdir D:/htdocs/SelfBuildRegister/] internal redirect with /user/verify/ [INTERNAL REDIRECT]
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e3300/initial/redir#1] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/ -> user/verify/
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e3300/initial/redir#1] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/reset/([^/\\.]+)/?$' to uri 'user/verify/'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e3300/initial/redir#1] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/ -> user/verify/
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e3300/initial/redir#1] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/verify/([^/\\.]+)/?$' to uri 'user/verify/'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace1] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e3300/initial/redir#1] [perdir D:/htdocs/SelfBuildRegister/] pass through D:/htdocs/SelfBuildRegister/user/verify/
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503d82a0/subreq] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/index.php -> user/verify/index.php
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503d82a0/subreq] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/reset/([^/\\.]+)/?$' to uri 'user/verify/index.php'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503d82a0/subreq] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/index.php -> user/verify/index.php
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503d82a0/subreq] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/verify/([^/\\.]+)/?$' to uri 'user/verify/index.php'
[Wed Jan 23 20:02:17.796472 2019] [rewrite:trace1] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503d82a0/subreq] [perdir D:/htdocs/SelfBuildRegister/] pass through D:/htdocs/SelfBuildRegister/user/verify/index.php
[Wed Jan 23 20:02:17.796472 2019] [http:trace3] [pid 13168:tid 1312] http_filters.c(1125): [client ::1:56417] Response sent with status 200
[Wed Jan 23 20:02:17.812095 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56417] AH01626: authorization result of Require all granted: granted, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56417] AH01626: authorization result of <RequireAny>: granted, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [core:trace3] [pid 13168:tid 1312] request.c(302): [client ::1:56417] request authorized without authentication by access_checker_ex hook: /css/sbr.css, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503de2d0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/css/sbr.css -> css/sbr.css, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503de2d0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/reset/([^/\\.]+)/?$' to uri 'css/sbr.css', referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503de2d0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/css/sbr.css -> css/sbr.css, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503de2d0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/verify/([^/\\.]+)/?$' to uri 'css/sbr.css', referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [rewrite:trace1] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503de2d0/initial] [perdir D:/htdocs/SelfBuildRegister/] pass through D:/htdocs/SelfBuildRegister/css/sbr.css, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:17.812095 2019] [http:trace3] [pid 13168:tid 1312] http_filters.c(1125): [client ::1:56417] Response sent with status 304, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56417] AH01626: authorization result of Require all granted: granted, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56417] AH01626: authorization result of <RequireAny>: granted, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [core:trace3] [pid 13168:tid 1312] request.c(302): [client ::1:56417] request authorized without authentication by access_checker_ex hook: /user/verify/themes/shepway/images/seperator.png, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] add path info postfix: D:/htdocs/SelfBuildRegister/user/verify/themes -> D:/htdocs/SelfBuildRegister/user/verify/themes/shepway/images/seperator.png, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/themes/shepway/images/seperator.png -> user/verify/themes/shepway/images/seperator.png, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/reset/([^/\\.]+)/?$' to uri 'user/verify/themes/shepway/images/seperator.png', referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] add path info postfix: D:/htdocs/SelfBuildRegister/user/verify/themes -> D:/htdocs/SelfBuildRegister/user/verify/themes/shepway/images/seperator.png, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/verify/themes/shepway/images/seperator.png -> user/verify/themes/shepway/images/seperator.png, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/verify/([^/\\.]+)/?$' to uri 'user/verify/themes/shepway/images/seperator.png', referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [rewrite:trace1] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56417] ::1 - - [localhost/sid#1d54f248728][rid#1d5503e22f0/initial] [perdir D:/htdocs/SelfBuildRegister/] pass through D:/htdocs/SelfBuildRegister/user/verify/themes, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [core:info] [pid 13168:tid 1312] [client ::1:56417] AH00128: File does not exist: D:/htdocs/SelfBuildRegister/user/verify/themes/shepway/images/seperator.png, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:18.187169 2019] [http:trace3] [pid 13168:tid 1312] http_filters.c(1125): [client ::1:56417] Response sent with status 404, referer: http://localhost:8282/user/verify/35435345
[Wed Jan 23 20:02:30.882218 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56447] AH01626: authorization result of Require all granted: granted
[Wed Jan 23 20:02:30.882218 2019] [authz_core:debug] [pid 13168:tid 1312] mod_authz_core.c(806): [client ::1:56447] AH01626: authorization result of <RequireAny>: granted
[Wed Jan 23 20:02:30.882218 2019] [core:trace3] [pid 13168:tid 1312] request.c(302): [client ::1:56447] request authorized without authentication by access_checker_ex hook: /user/reset/35435345
[Wed Jan 23 20:02:30.882218 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56447] ::1 - - [localhost/sid#1d54f248728][rid#1d5503da2b0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/reset/35435345 -> user/reset/35435345
[Wed Jan 23 20:02:30.882218 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56447] ::1 - - [localhost/sid#1d54f248728][rid#1d5503da2b0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/reset/([^/\\.]+)/?$' to uri 'user/reset/35435345'
[Wed Jan 23 20:02:30.882218 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56447] ::1 - - [localhost/sid#1d54f248728][rid#1d5503da2b0/initial] [perdir D:/htdocs/SelfBuildRegister/] strip per-dir prefix: D:/htdocs/SelfBuildRegister/user/reset/35435345 -> user/reset/35435345
[Wed Jan 23 20:02:30.882218 2019] [rewrite:trace3] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56447] ::1 - - [localhost/sid#1d54f248728][rid#1d5503da2b0/initial] [perdir D:/htdocs/SelfBuildRegister/] applying pattern '^user/verify/([^/\\.]+)/?$' to uri 'user/reset/35435345'
[Wed Jan 23 20:02:30.882218 2019] [rewrite:trace1] [pid 13168:tid 1312] mod_rewrite.c(482): [client ::1:56447] ::1 - - [localhost/sid#1d54f248728][rid#1d5503da2b0/initial] [perdir D:/htdocs/SelfBuildRegister/] pass through D:/htdocs/SelfBuildRegister/user/reset/35435345
[Wed Jan 23 20:02:30.882218 2019] [core:info] [pid 13168:tid 1312] [client ::1:56447] AH00128: File does not exist: D:/htdocs/SelfBuildRegister/user/reset/35435345
[Wed Jan 23 20:02:30.882218 2019] [http:trace3] [pid 13168:tid 1312] http_filters.c(1125): [client ::1:56447] Response sent with status 404

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Dr. Klahn

Oh, this is a file URL!

Sorry, I have no knowledge of how those work as opposed to normal HTTP URLs.  Looking at the log there are some things I'd not expect to see in an HTTP URL.

I've no doubt that another expert who has dealt with this before will be able to help after they've seen the posting.
Neil Thompson

ASKER
it shouldn't be a file URL.

It should simply be when I enter for example http://localhost:8282/user/reset/35435345 in the background it should be sending http://localhost:8282/user/reset/?rp=35435345

As I say then first one always does as it should.
Neil Thompson

ASKER
Weirdly, I've just had another play and removed the bit in bold and it works, aaagh!

Thanks for all your assistance though, the logging was something I'd never thought of before.

Regards
Neil

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$  


# allows reset URL like /user/reset/5d34f4e59c155566913f782a2e2557dc,234j5hj23k452hk
RewriteRule ^user/reset/([^/\.]+)/?$      user/reset/?rp=$1

# allows verification URL like /user/verify/5d34f4e59c155566913f782a2e2557dc
RewriteRule ^user/verify/([^/\.]+)/?$      user/verify/?uvc=$1

ErrorDocument 404 404.php
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.