Forum Moderators: phranque
https://www.example.com/bboard/cgi-bin//ultimatebb.cgi?ubb=get_profile;u=00009498 https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=00009498 RewriteCond “%{REQUEST_URI}” “/bboard/cgi-bin/ultimatebb.cgi”
RewriteCond “%{QUERY_STRING}” “ubb=showprofile&User=(\d*)”
RewriteRule ^/bboard.*.cgi$ /ubbthreads/ubbthreads.php?ubb=showprofile&User=%1” [edited by: phranque at 9:18 pm (utc) on Jul 6, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
(It doesn't work.)
RewriteCond “%{REQUEST_URI}” “/bboard/cgi-bin/ultimatebb.cgi”
RewriteCond “%{QUERY_STRING}” “ubb=showprofile&User=(\d*)”
RewriteRule ^/bboard.*.cgi$ /ubbthreads/ubbthreads.php?ubb=showprofile&User=%1”
RewriteCond %{QUERY_STRING} ^ubb=get_profile;u=(\d+)$
RewriteRule ^bboard/cgi-bin/ultimatebb\.cgi$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
https://www.example.com/bboard/cgi-bin//ultimatebb.cgi?ubb=get_profile;u=00009498A quirk of mod_rewrite is that RewriteRules ignore extra slashes, but RewriteConds take them as written. (I know this only because I once made an error that led to links containing // in the middle. I had to redirect them just so search engines would stop asking.)
LogLevel alert rewrite:trace3 [Wed Jul 06 22:55:09.804734 2022] [rewrite:trace3] [pid 15176] mod_rewrite.c(470): [client 157.55.39.87:43010] 157.55.39.87 - - [example.com/sid#55fb03b7d378][rid#55fb03e6b750/initial] applying pattern '^bboard/cgi-bin/ultimatebb\\.cgi$' to uri '/ubbthreads/ubbthreads.php/topics/1456461/possible-grand-piano-for-free.html'
[Wed Jul 06 22:55:09.804846 2022] [rewrite:trace3] [pid 15176] mod_rewrite.c(470): [client 157.55.39.87:43010] 157.55.39.87 - - [example.com/sid#55fb03b7d378][rid#55fb03e7aed0/subreq] applying pattern '^bboard/cgi-bin/ultimatebb\\.cgi$' to uri '/topics/1456461/possible-grand-piano-for-free.html'
[Wed Jul 06 22:55:09.824370 2022] [rewrite:trace3] [pid 15176] mod_rewrite.c(470): [client 157.55.39.87:43010] 157.55.39.87 - - [example.com/sid#55fb03b7d378][rid#55fb03eadd50/subreq] applying pattern '^bboard/cgi-bin/ultimatebb\\.cgi$' to uri '/topics/1456461/possible-grand-piano-for-free.html'
[Wed Jul 06 22:55:09.824585 2022] [rewrite:trace3] [pid 15176] mod_rewrite.c(470): [client 157.55.39.87:43010] 157.55.39.87 - - [example.com/sid#55fb03b7d378][rid#55fb03eadd50/subreq] applying pattern '^bboard/cgi-bin/ultimatebb\\.cgi$' to uri '/topics/1456461/possible-grand-piano-for-free.html' # tail -f /var/log/httpd/example/error.log | grep "70.121.63.82" # tail -f /var/log/httpd/example/ssl_access_log | grep "70.121.63.82"
70.121.63.82 - - [06/Jul/2022:23:07:19 -0400] "GET /bboard/cgi-bin//ultimatebb.cgi?ubb=get_profile;u=00012520 HTTP/1.1" 404 227
70.121.63.82 - - [06/Jul/2022:23:07:26 -0400] "-" 408 - [edited by: phranque at 7:50 am (utc) on Jul 7, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
That must be something the board does, because I posted the actual links.Welcome to WebmasterWorld ;-)
RewriteCond “%{REQUEST_URI}” “/bboard/cgi-bin/ultimatebb.cgi”
RewriteCond “%{QUERY_STRING}” “ubb=showprofile&User=(\d*)”
why is everything inside quotation marks? And, equally important, are they really curly quotes, or is that an artifact of copy-paste for posting?
why is everything inside quotation marks? And, equally important, are they really curly quotes, or is that an artifact of copy-paste for posting?
RewriteCond "%{QUERY_STRING}" "ubb=.*u=(\d*)"
RewriteRule "^bboard/cgi-bin/ultimatebb\.cgi$" "https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1" [L,R=301] [edited by: phranque at 7:50 am (utc) on Jul 7, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
The new url is /ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 (I removed the https:// and www.example.com so it would be visible.)
Test are stopped, a redirect will be made with status code 301
[edited by: not2easy at 4:42 pm (utc) on Jul 8, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
RewriteCond %{QUERY_STRING} ubb=.*u=(\d*)
RewriteRule ^bboard/.*$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] 1 RewriteCond %{QUERY_STRING} ubb=.*u=(\d*)
This condition was met.
2 RewriteRule ^bboard/.*$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
The new url is https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=00009498
Test are stopped, a redirect will be made with status code 301 # grep Rewrite /etc/httpd/conf.d/example.conf
RewriteEngine On
RewriteOptions Inherit
RewriteCond %{QUERY_STRING} ubb=.*u=(\d*)
RewriteRule ^bboard/.*$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] # tail -f /var/log/httpd/example/error.log | grep rewrite | grep bboard
[Thu Jul 07 03:31:16.017080 2022] [rewrite:trace3] [pid 623] mod_rewrite.c(470): [client 157.55.39.49:2688] 157.55.39.49 - - [example.com/sid#55f21d4d12f8][rid#55f21d7d0ec0/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/topics/1457572/installing-gauges.html'
[Thu Jul 07 03:32:03.541122 2022] [rewrite:trace3] [pid 626] mod_rewrite.c(470): [client 157.55.39.87:41921] 157.55.39.87 - - [example.com/sid#55f21d4d12f8][rid#55f21d7ceeb0/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/topics/1457113/re-just-a-quick-heads-up.html'
RewriteCond %{QUERY_STRING} ubb=get_profile.*u=(\d*) and now I'm testing some more. [edited by: not2easy at 4:43 pm (utc) on Jul 8, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
I'm gettting comparisons against the pattern in the rewrite rule to urls that don't meet the RewriteCond. How is that even possible?
Here's what's in the conf file now. And yes, I restarted Apache. After confirming that there were no errors using apachectl configtest.
# grep Rewrite /etc/httpd/conf.d/example.conf
RewriteEngine On
RewriteOptions Inherit
RewriteCond %{QUERY_STRING} ubb=.*u=(\d*)
RewriteRule ^bboard/.*$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
So, something else is wrong.
RewriteRule ^/bboard/ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] RewriteCond %{QUERY_STRING} ^ubb=.+;u=(\d+)$
The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.
[Thu Jul 07 08:26:18.183738 2022] [rewrite:trace3] [pid 17347] mod_rewrite.c(470): [client 157.55.39.98:35008] 157.55.39.98 - - [example.com/sid#55d672bca2f8][rid#55d672eb46f0/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/topics/1457624/re-how-to-troubleshoot-truck-not-turning-over.html'
[Thu Jul 07 08:26:29.798420 2022] [rewrite:trace3] [pid 17183] mod_rewrite.c(470): [client 207.46.13.184:31360] 207.46.13.184 - - [example.com/sid#55d672bca2f8][rid#55d672f06bc0/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/users/8659.html'
[Thu Jul 07 08:26:40.013497 2022] [rewrite:trace3] [pid 17534] mod_rewrite.c(470): [client 207.46.13.184:31360] 207.46.13.184 - - [example.com/sid#55d672bca2f8][rid#55d672eb46f0/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/topics/1457553/pto-needed-sm420.html'
RewriteCond %{QUERY_STRING} ubb=get_profile.*u=(\d*)
RewriteRule ^bboard/.*$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] tail -f /var/log/httpd/stovebolt/error.log | grep rewrite | grep bboard
[Thu Jul 07 08:32:12.115951 2022] [rewrite:trace3] [pid 17279] mod_rewrite.c(470): [client 207.46.13.184:32000] 207.46.13.184 - - [example.com/sid#55d672bca2f8][rid#55d672eb8710/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/topics/1457454/ad-wiper-blade.html'
[Thu Jul 07 08:33:14.696501 2022] [rewrite:trace3] [pid 17945] mod_rewrite.c(470): [client 157.55.39.49:8384] 157.55.39.49 - - [example.com/sid#55881ce3e2f8][rid#55881d128730/initial] applying pattern '^bboard/.*$' to uri '/ubbthreads/ubbthreads.php/topics/1457548/re-1937-chevy-1-5-ton-dually-disc-brake-conversion.html' [edited by: not2easy at 6:23 pm (utc) on Jul 7, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
the RewriteRule Pattern is matched before any RewriteCond directives within the ruleset.
The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.
since this is in config context rather than directory context, the RewriteRule Pattern must start with the leading slash (i.e. specifying the document root directory):RewriteRule ^/bboard/ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301
also note that if you aren't capturing the trailing .*, it and the end anchor are unnecessary in the Pattern specification.I am capturing the trailing .*. The entire point of this is to capture the member ID and reuse it in a different URI.
i would also suggest using the most specific RewriteRule and conditional Pattern specifications possible:That's good advice, because the pattern I was using was matching other uris that I wouldn't want to match. Thus the errors in my logs that I posted previously.
[edited by: not2easy at 4:44 pm (utc) on Jul 8, 2022]
[edit reason] delinked unrelated tool [/edit]
I am capturing the trailing .*. The entire point of this is to capture the member ID and reuse it in a different URI.You're confusing two different .*
When I tested the pattern on [htaccess.madewithlove.com ] adding the leading forward slash breaks the rule.Well, yeah. That's because yours is a config file rule and the tool is made to test htaccess. Even then, htaccess testers aren't perfect, as shown by the literal "%1" in the result. This string should have been replaced by either some content, or none, depending on whether there was a successful capture--and if the rule isn't applied at all, then there wouldn't be a target of any kind.
RewriteCond %{QUERY_STRING} ubb=get_profile.*u=(\d*)
RewriteRule /bboard/cgi-bin/.* https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] RewriteCond %{QUERY_STRING} ubb=get_profile.*u=(\d*)$
RewriteRule /bboard/cgi-bin/ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] [edited by: not2easy at 6:29 pm (utc) on Jul 7, 2022]
[edit reason] Please Use example.com For Domain Names in Posts Apache Web Server forum [/edit]
I added the /cgi-bin/ just to be on the safe side.
ubb=get_profile;u=00009498
meaning that the pattern would be ubb=get_profile;u=(\d+)
It may seem trivial, but any time a Regular Expression meets a .* or .+ it then gobbles up* everything to the end--and then, if there is something after the .* or .+ it has to backtrack until it finds a match for the remaining bit. RewriteCond %{QUERY_STRING} ubb=get_profile.*u=(\d*)
RewriteRule ^bboard/.*$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{QUERY_STRING} ubb=get_profile;u=(\d*)
RewriteRule /bboard/cgi-bin/ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] [edited by: not2easy at 6:32 pm (utc) on Jul 7, 2022]
[edit reason] Please Use example.com For Domain Names in Apache Web Server forum [/edit]
RewriteCond %{QUERY_STRING} ubb=get_profile;u=(\d*)
RewriteRule /bboard/cgi-bin/ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{QUERY_STRING} ^ubb=get_profile;u=(\d+)$
RewriteRule ^/bboard/cgi-bin/ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
I thought perhaps setting AllowOverride to All in the main httpd.conf file was what fixed the problemThe AllowOverride directive applies to one thing and one thing only: the use of htaccess files. The directive really means what it sounds like: allow some other file to override the rules laid out in the config file. It has no effect whatsoever on things that are in config anyway. But if you don't need or want htaccess files, leave AllowOverride set at None, because then the server does not have to take the time to look for htaccess files on every ... single ... request.
Can you tell me what the required differences are between RewriteCond and RewriteRule in .htaccess versus conf file?
What is matched?
- In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
- In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.
The directory path where the rule is defined is stripped from the currently mapped filesystem path before comparison (up to and including a trailing slash). The net result of this per-directory prefix stripping is that rules in this context only match against the portion of the currently mapped filesystem path "below" where the rule is defined.
Directives such as DocumentRoot and Alias, or even the result of previous RewriteRule substitutions, determine the currently mapped filesystem path.
RewriteEngine On
RewriteOptions Inherit
RewriteRule ^links/ /links.html [L,R=301]
RewriteCond %{QUERY_STRING} ubb=get_profile;u=(\d+)\b
RewriteRule ^bboard/.*$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{QUERY_STRING} page=get_profile;u=(\d+)\b
RewriteRule ^bboard/.*$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{QUERY_STRING} page=editprofile;u=(\d+)\b
RewriteRule ^bboard/.*$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{REQUEST_URI} /bboard/cgi-bin/ultimatebb.cgi
RewriteRule (.*)$ https://www.example.com//ubbthreads/ubbthreads.php [L,R=301]
RewriteCond %{QUERY_STRING} page=getprofile;u=(\d+)\b
RewriteRule (.*)$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301] [edited by: not2easy at 3:56 pm (utc) on Jul 8, 2022]
[edit reason] Please see TOS [webmasterworld.com] [/edit]
These rules work in the .htacess file but do not work in the conf file. What do I have to change to get them to work in the conf file?
In config, patterns start with the whole server. So you have to either omit the opening anchor, or include the full physical filepath.
RewriteRule ^links/ /links.html [L,R=301] RewriteRule /links/ /links.html [L,R=301]
Pro tip: Leave a blank line between each ruleset (RewriteRule plus attached Conditions). This has no syntactic meaning, but will make it vastly easier for you, the human site administrator, to keep track.I did this immediately after I read this, and I added comments regarding what the purpose of each rule was.
As previously noted, there is never any reason for the non-capturing element
.*$
since it simply means “there might or might not be more stuff after the pattern” which is understood anyway. May as well let the server get out of there a few picoseconds sooner.
read the Apache doc re:Context and then you will understand why and how patterns will differ between .htaccess directives and config file directives:
[httpd.apache.org...]
I'm headed there now. I confess that Apache docs are often too technical for me to understand, but I try my best.