Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Need PHP/Nginx help (dynamic urls)
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Need PHP/Nginx help (dynamic urls)

Ok so I have a basic php driven site that I'm working on for fun, it's just being ran on my raspberry pi right now.

This is my index.php file: https://pastebin.com/u3AfJeH2

This "works", and allows me to have a basic design template and I don't need to update every page I create if I want to change a link in the navigation or footer or something. It loads the content that I want (see array) like this:

http://xx.xx.xx.xx/index.php?p=something

that will load the content that is in 'something.php' . http://xx.xx.xx.xx/index.php?p=test will of course load the content that is in 'test.php' and it's all done within the design of the page so I don't have to make a bunch of different pages or updates to them, just the content files.

Okay so that works fine, but I want the URLs to be 'pretty' and I can not get it to work at ALL! So frustrating.

My nginx file looks like:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.php index.html index.htm;

        server_name _;

        location / {
                rewrite ^/(.*)$ /index.php?p=$1;
        }

        #location / {
        #       try_files $uri $uri/ =404;
        #}

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

That "kind of works", like, I can go to http://xx.xx.xx.xx/test instead of http://xx.xx.xx.xx/index.php?p=test BUT all styling and images stop loading, and I'm not sure why. If I view the output html source from the pages that are loading using the above configuration, I can see that the path to these elements have remain unchanged but they will not load so it breaks all styling and appearances.

If I comment out

        location / {
                rewrite ^/(.*)$ /index.php?p=$1;
        }

then those elements/images will work again, but then I can't use the url structure I want and I'm back where I started.

Comments

  • hzrhzr Member
    edited December 2020

    Probably because you are redirecting /whatever.css to /index.php?p=whatever.css also

    rewrite ^/([a-z]+)$ will probably be a better capture if you just want "something" "test" and other alpha, and won't capture everything.extension

    change to a-zA-Z0-9 if you want alphanumerics in large

    Thanked by 1Rambler
  • @hzr said:
    Probably because you are redirecting /whatever.css to /index.php?p=whatever.css also

    rewrite ^/([a-z]+)$ will probably be a better capture if you just want "something" "test" and other alpha, and won't capture everything.extension

    change to a-zA-Z0-9 if you want alphanumerics in large

    That worked.

    I knew I was close. Been staring at a screen too long today. I think I need to go for a walk and clear my head.

    Thank you @hzr for your assistance.

Sign In or Register to comment.