Correct way to have Seo freindly urls


#1

Can someone show me the correct way to have seo freindly URL’s in an HTML site. I have php site in ee.

What is right way to get nginx to serve /about.html for a request for domain.com/about

I am using --php because of php contact form.

Thanks


#2

I ended up editing …common/php.conf

location / {
  try_files $uri $uri/ $uri.html /index.php?$args;
    if ($request_uri ~ ^/(.*)\.html$) {
        return 302 /$1;
    }

}

If this is going to set the world ablaze please let me know. Worried it will be overwritten.


#3

I don’t think that’s going to do what you want. For a URL of /about, the try_files directive would look in order for these files: about about/ about.html

And if none of those are found it will redirect the request to index.php. So I don’t think it will ever reach your next statement.

Are you trying to serve all urls without an extension but write them to php?


#4

Sorry for delay Marty. I was just trying to get php contact form working with php and postfix. I never could get php7 to mail the damn thing out. Everything seems to be working as expected. Nginx knows how to find requests from urls with the html extension and serves them purdy.

There is no index.php just index.html. All to get the php contact form working with postfix.

edit:

played around with this a bit more today.

reverted common/php.conf back to way it should be and created cononical.conf in webroot or whatever ee calls it. /var/www/example.com/conf/nginx/cononical.conf

with

if ($request_uri ~ ^(.*/)index(?:\.html)?$) {
    return 301 $1;
}

if ($request_uri ~ ^(.+)\.html$) {
    return 301  $1;
}

try_files $uri $uri.html =404;

this is working nice and not using 302. My urls are pretty, my ifs are out of location block and php.conf can be overwritten.

thoughts?


#5

This looks kind of wrong :slight_smile: . ‘If’ statements are generally a bad solution with nginx (inefficient and rarely necessary - there are exceptions).

I’m still not really sure what you’re trying to achieve though :slight_smile:

If you’re doing 301’s (or 302’s) then you’re telling the search engines that this is not the correct page address, it’s something else, so go here instead…

So what URL’s are in your site navigation - is it /page/ or /page.html? Because if everything is html except your contact page, then you can just use /page.html for everything - and only rewrite (NOT redirect) /contact.html to /contact.php (the user would still see contact.html in their address bar but php processes just those requests).

Or you can get rid of all file extensions (.html, .php) from the URL if that’s what you want?


#6

Yes I am looking to remove the .html and strip index.html from /. I see your reasoning with the 301’s. The only php on the site is the contact form backend which wouldn’t fire off unless I used --php.

Any insight appreciated, thank you for your time. Right now the urls are displaying /page . which is what I was looking for.

Thanks again Marty, hows this look? Working as desired

rewrite ^/(.*)/$ /$1 permanent;

Then I just changed my nav links to href=“page” from href=“page.html” and / for root.

is the permanent necessary or should I remove it?


#7