Viewed   192 times

I have a rewrite in nginx or Apache for this address:

http://domain.com/hello

to a script like

http://domain.com/test.php&ref=hell

How can I access this rewritten URL in PHP? Because, if I use $_SERVER['REQUEST_URI'] of course I get:

/test.php&ref=hell

but I only want:

/hello

Is this possible? Thanx for help.

Upd nginx cnf

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server
{
  listen 80;
  server_name domain.test;


  location /
  {
    rewrite ^/(main|best|air)$ /core/feeds.php?act=$1 last;
    proxy_pass http://127.0.0.1:8080;
  }
}

 Answers

1

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']
Wednesday, December 21, 2022
5

Check if your apache installation has mod_rewrite enabled.

In its simplest form, the rule to accomplish what you want to do would be:

In your .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ http://mysite.com [R=301,L]

RewriteRule songs/(.*)/(.*)$ songs/index.php?name=$1&song=$2

But give it a read to the apache documentation http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html as well as verify your configuration, and here you'd find some useful stuff as well http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#redirects

Hope that helps.

Monday, September 19, 2022
 
zarah
 
5

In the end I had the guy who manages the Apache proxy server to add ProxyPreserveHost On to apache2.conf and this enabled forwarding of HTTP_HOST (and SERVER_NAME) properly to the backend Apache server.

Thank you everyone for your suggestions, I'm sorry the problem ended up being something stupid that wasn't even in the scope of my question. If the Apache server I was working on did not have a proxy in front of it, most of your suggestions would have proven completely accurate.

Monday, November 28, 2022
 
3

Have it this way:

Options +FollowSymLinks
RewriteEngine on

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

RewriteCond %{THE_REQUEST} /index.html [NC]
RewriteRule ^index.html$ /? [R=301,L,NC]

RewriteRule ^listen/$ /console/ [NC,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

Test it after clearing your browser cache.

Saturday, December 24, 2022
1

The matches are zero based.

<action type="Rewrite" url="https://abc.com/{R:1}" />

Won't work because you only have one match. You need:

<action type="Rewrite" url="https://abc.com/{R:0}" />

Also, this won't work, because you can only match on the path below the site root.

<match url="^(?!https://).*" ignoreCase="false" />

It looks like you are checking for ssl. Try this instead:

      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
Wednesday, December 7, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :