Apache rewrite - get original URL in PHP
In Nginx
conf, we need to add user header with request_uri
:
proxy_set_header request_uri $request_uri;
And read it in php
:
echo $_SERVER['HTTP_REQUEST_URI'];
upd
for some reason nginx don't like symbol '_' in header name, don't know how it worked before, maybe something changed after nginx update. Now i'm using
proxy_set_header rewriteduri $request_uri;
and in php
$_SERVER['HTTP_REWRITEDURI']
Is it possible to get the original URL after a 302 .htaccess RedirectMatch?
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]
How to redirect a page but keep the original URL with htaccess
You need to drop the R
flag as it's designed for external redirects and this does not preserve the original URL. What you need is an internal redirect.
You also need to use QSA
flag to keep the query string, if needed. It's there by default. So:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^s/([a-zA-Z_-]+)$ /s/ [L]
</IfModule>
But there's probably a problem here; you're missing the passed data (e.g. united-states
). You probably want that to be mapped to your PHP script. To pass that as a querystring, you can do something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^s/([a-zA-Z_-]+)$ /s/?country=$1 [L,QSA]
</IfModule>
This way you're merging a new query string parameter (country
) with all existing QS parameters, if any.
One more thing; you won't need a IndexIgnore *
directive with Options -Indexes
.
Rewrite parameter url to normal url but it should get original url parameters after redirect
With your shown samples and attempts, please try following htaccess rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect here.
RewriteCond %{THE_REQUEST} \s/all_users/user\.php\?id=\d+&subtype=p\s [NC]
RewriteRule ^ user/john-doe.html? [R=301,L]
##Internal rewrite from here..
RewriteRule ^user/john-doe\.html/?$ all_users/user.php?id=1&subtype=p [QSA,NC,L]
rewrite URL .HTACCESS redirecting to original URL which should not
if stores/mystore
points to a directory then mod_dir
will add a trailing slash after your rewrite rules have executed.
Try these rules:
Options +FollowSymLinks
RewriteEngine On
# add trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !(home|seller)
RewriteRule ^([^/]+)/?$ stores/$1 [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.+[^/]$ %{REQUEST_URI}/ [L]
htaccess to rewrite the original URL
you must have mod_rewrite enabled, then you can try:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ notfound.php [L]
the first two lines ensure that you can still load things that exist (like js and css files that you may need)
URL rewrite rule is not working in php
Use this source after you make sure RewriteEngine Enabled
and On
RewriteEngine On
RewriteRule ^events/([^/]*)$ /events/landing_event.php?url=$1 [L]
The original URL:
http://www.example.com/events/landing_event.php?url=text-first
The rewritten URL:
http://www.example.com/events/text-first
Updated with 500 Internal Server Error
Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^events/([^/]*)$
Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.
Change your code to this
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]*)$ /events/landing_event.php?url=$1 [L,QSA,NC]
Related Topics
Is There a Difference Between !== and != in PHP
Remove Empty Tags from a Xml with PHP
What Delimiter to Use for Preg_Replace in PHP (Replace Working Outside of PHP But Not Inside)
Count Specific Values in Multidimensional Array
Nested Array. Third Level Is Disappearing
Can't Pass MySQLi Connection in Session in PHP
Fatal Error: Using $This When Not in Object Context
Looping a Multidimensional Array in PHP
Array_Multisort and Dynamic Variable Options
How to Detect an Incoming Request (With PHP Script) from a Cname Subdomain
Check If Specific Array Key Exists in Multidimensional Array - PHP
What's the Disadvantage of Mt_Rand
Geo Location Based on Ip Address - PHP