What is a good way to have a SEO URL system built

2019-03-22 06:20发布

I want to have "pretty" and SEO oriented URLs in my site.

I've build up my own tiny framework for this site and almost everything is complete now.

One thing I'm still puzzled up is the pretty/SEO URLs system that I will use. I know there's many way to achieve this and I'm looking to balance best practices/ease of implementation on this one.

So far I'm thinking to have all URLs of the site to point to a specific PHP file (let's say index.php) that will contain a file/URL dictionary that will direct traffic to the correct file.

I'm really not sure if it's a good approach... Anyone have a better way to do this? The only thing I really want to avoid is to only do this in an .htaccess...

9条回答
小情绪 Triste *
2楼-- · 2019-03-22 06:51

Well, there are some ways to do this.

Widely adopted is using .htaccess, which you said don't wanna use. OK.

You still have some options:

  • Using a database table to map everything is one.
  • Using a routine to check existing files.
  • Using a extended xml sitemap.
  • Using a caching system.

Well, everything above can be mixed on your implemetation if you want.

You can find a good article for this on a list apart.

http://www.alistapart.com/articles/succeed/

On this article the solution is a mix:

first check if a file exists (for example "about-us.php"); if yes, include the file contents, else

check db for this request (as a tip, you can have a field named "friendlyURL" on your main content tables). if exists, extract and display, else

show a 404 page. as a tip for this one, keeping the SEO feature in mind, I would recommend you to have a sitemap xml. If page is not found, you can check sitemap if is not a broken URL, like:

http://yourdomain.net/shoes/male/bro

you can check if some URL like: http://yourdomain.net/shoes/male/brown

and suggest it to your customers/visitors. Along with:

http://yourdomain.net/shoes/male/

http://yourdomain.net/shoes/

also a link for your HTML sitemap, and if you have a search feature on your site, also use it, display a link for user go to search page with that query.

http://yourdomain.net/search?q=shoes+male+bro

OR

[input type="text" name="q" value="shoe+male+bro"];

And another extra tech tip: make use of full-text search feature of your db if available.

A interesting reading comes from Rasmus Lerdorf, PHP creator: http://lerdorf.com/lca04.pdf (check page 34, about 404 redirects).

查看更多
神经病院院长
3楼-- · 2019-03-22 07:02

Very pretty routing is in Nette Framework see nette.org. Like your idea, every url goes to index.php. And there is routers, which direct what will happen, etc. http://api.nette.org/2.0/Nette.Application.SimpleRouter.html

查看更多
Lonely孤独者°
4楼-- · 2019-03-22 07:05

Just to add another alternative, you might want to check out the RewriteMap directive.

查看更多
祖国的老花朵
5楼-- · 2019-03-22 07:06

I think you said that the pages could be static, right? Well, a solution that I use sometimes is:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9-_]+)/?$ files/$1.php [NC,L]
RewriteRule ^$ files/index.php [NC,L]

This way, you can't run into issues like people accesing files like .htpasswd or sensitive data. The page /about would lead to /files/about.php

Create your files directory and new PHP (or HTML, XML, whatever - just change it or add it in the .htaccess as well) files. Nothing else than that.

Let me know if this is good wnough for you :)

P.S: If the pages have content from the database (like articles on a blog) it could be even easier to use an extra parameter and stick with .htaccess:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ files/article.php?id=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9-_]+)/?$ files/$1.php [NC,L]
RewriteRule ^$ files/index.php [NC,L]

This way, the rules I said first time apply, but when you have an URL like /3/my_new_article it would point to /files/article.php?id=3

You would also have to parse the title of the article with PHP so it would only accept the characters in the regex, look nice and also contain only valid URL characters if you plan to change the regex.

Cheers!

查看更多
Explosion°爆炸
6楼-- · 2019-03-22 07:06

you are probably looking for something like mod-rewrite that way you would have a link that looks like http://my.server.com/my_file_of_this_name
and with mod-rewrite it would turn it into http://my.server.com/index.php?path=my_file_of_this_name
where index.php knows that if path was a passed variable, look in the DB for the content or page matching 'my_file_of_this_name' and either display it or header("location: ???) it

查看更多
地球回转人心会变
7楼-- · 2019-03-22 07:08

If you want a seo urls there is a pretty good function to do with(out) htaccess ! it will be used as $_POST & $_GET . use it as $_SEO !! .. it uses /query-value/query2-value2/ $_SEO['query'] will return 'value'

check it at : http://mohammed-alashaal.blogspot.com/2013/07/php-seo-uri-without-htaccess.html

查看更多
登录 后发表回答