logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in  RSS Apache Lounge  


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.


Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: htaccess rule doesnt catch some url
Author
arakpt



Joined: 25 Jul 2021
Posts: 8
Location: jakarta

PostPosted: Sat 11 Sep '21 15:51    Post subject: htaccess rule doesnt catch some url Reply with quote

hi

i have set of 404 url and htaccess rule as follow

Code:
RewriteCond %{QUERY_STRING} (?:^|&)(amp|m|utm_\w+)=
RewriteRule ^(.*)$ /$1? [R=301,L]


my 404 url are something like this

Quote:
http://domain.com/post1.html?amp
http://domain.com/post1.html?amp=1
http://domain.com/post1.html?m=1
http://domain.com/post1.html?utm_source=feedburner


so my goal is all 404 url having query in the end url will redirect to the post url
Quote:
http://domain.com/post1.html?amp=1 >> 301 http://domain.com/post1.html
http://domain.com/post2.html?m=1 >> 301 http://domain.com/post2.html
http://domain.com/post3.html?utm_source=feedburner >> 301 http://domain.com/post3.html


My problem is the rule above doesnt seems to catch ?amp (without = char),can someone help me with the correct code?

Secondly is it possible to put the rules directly on vhost.conf instead using htacces

thank you
Back to top
tangent



Joined: 16 Aug 2020
Posts: 131
Location: UK

PostPosted: Sat 11 Sep '21 22:34    Post subject: Reply with quote

Depending on what field separators you expect between parameters in your query string, and the position of those parameters, your regex is incomplete. Moreover, for your posted problem if you need to catch the case where the equal sign is optional after the match sub-string, you need an additional "zero of more" case regex parameter, viz:

Code:
RewriteCond %{QUERY_STRING} (\?(|.*?[:^|&]*?))(amp|m|utm_\w+)(=?)

Note the revised regex construct after the initial "? query string character, to cope with various field separator characters. I've also added a couple of '?' characters to make the wild card match strings non-greedy.

Does this do what you need?

Note this regex construct won't cope with all query string constructs (it's late evening here, so forgive me for not working this through fully).
Back to top
arakpt



Joined: 25 Jul 2021
Posts: 8
Location: jakarta

PostPosted: Sun 12 Sep '21 6:52    Post subject: Reply with quote

thank you tangent

But for some reason, it seems url having ?amp still return 404 not 301

Code:
http://domain.com/post1.html?amp
Back to top
tangent



Joined: 16 Aug 2020
Posts: 131
Location: UK

PostPosted: Sun 12 Sep '21 20:35    Post subject: Reply with quote

Ok, I spent some time looking at the regex, and came up with the following revised solution:
Code:
RewriteCond %{QUERY_STRING} \b(amp|m|utm_\w+)\b(=\w+)??
RewriteRule ^(.*)$ $1? [R=302,L]

(note I've removed the "/" in your RewriteRule too). I found this site helpful for testing regex constructs - https://regex101.com

One other issue maybe. You specified 301 (permanent) redirects, rather than 302's, and browsers will cache 301 decisions till their cache is cleared. To avoid unwanted behaviour, I therefore generally favour 302 redirects.

If I use command line curl against a local Apache test server, I now get the following results for your "amp=1" and "amp" use cases.
    C:\>curl http://192.168.1.8/post1.html?amp=1
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="http://192.168.1.8/post1.html">here</a>.</p>
    </body></html>

    C:\>curl http://192.168.1.8/post1.html?amp
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="http://192.168.1.8/post1.html">here</a>.</p>
    </body></html>
Back to top
arakpt



Joined: 25 Jul 2021
Posts: 8
Location: jakarta

PostPosted: Fri 24 Sep '21 15:52    Post subject: Reply with quote

Thank you very much tangent,your post history always helpfull to other
youre a life saver, the code works perfectly..
Back to top


Reply to topic   Topic: htaccess rule doesnt catch some url View previous topic :: View next topic
Post new topic   Forum Index -> Apache