Home Is it possible to get the original URL after a 302 .htaccess RedirectMatch?
Reply: 1

Is it possible to get the original URL after a 302 .htaccess RedirectMatch?

Andrei S.
1#
Andrei S. Published in 2017-12-07 12:52:57Z

Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.

My .htaccess currently looks like this:

RedirectMatch 302 ^/\w{5}$ /redir.php

The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.

As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.

EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.

RewriteRule ^\w{5}$ /redir.php [R,L]
mega6382
2#
mega6382 Reply to 2017-12-07 13:22:31Z

you can use the following rule:

RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]

this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:

$_GET['redir']

Edit: Or as @LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:

RewriteRule ^(\w{5})$ /redir.php [NC,L]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.332358 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO