What is a good way to have a SEO URL system built up in PHP?

前端 未结 3 1245
遥遥无期
遥遥无期 2021-02-11 07:16

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

相关标签:
3条回答
  • 2021-02-11 07:26

    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).

    0 讨论(0)
  • 2021-02-11 07:27

    You'll need an .htaccess file (but you won't need to change it each time you add a page):

    RewriteEngine On 
    #RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
    

    Now in your index.php you can access the requested url in $_GET['url'], map it to the correct file and then include it.

    Note: Put the RewriteBase comment in there in case you need to uncomment it as some configurations require this.

    0 讨论(0)
  • 2021-02-11 07:31

    Directing everything to index.php and then routing from there is a good, clean way to do it. I have something very similar, I:

    1. Route everything to index.php in .htacess.
    2. In index.php I split the url by '/' to get an array
    3. The first element of the array is the name of the class to call.
    4. The second element is the function of the class to call.
    5. If needed, remaining elements are parameters.

    For example, browsing to:

    www.blah.com/shop/browse/cakes

    Would call index.php, which would include shop.php and instantiate a class called Shop. Would try to call a function on Shop called browse and would pass it a parameter of "cakes".

    This is a simplified example but you get the idea. Convention over configuration makes the URLs and the code clean.

    0 讨论(0)
提交回复
热议问题