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

安稳与你 提交于 2019-12-12 07:41:56

问题


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


回答1:


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.




回答2:


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:


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.




回答4:


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!




回答5:


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




回答6:


An alternative to routing through your index.php page and the rewrite rules etc people have been posting is multiviews. It is by far the easiest way to do what you want and will let you convert your site to use pretty/seo urls really easily and intuitively without complex htaccess rules and having to maintain routings (in .htaccess or a db)/a complex site structure.

To enable multiviews, edit your httpd.conf or vhost file for the directory you want to enable it for: (if you're on shared hosting it should still be possible providing your host allows you to do a local override in your .htaccess file - Options +Multiviews)

<Directory /var/www/>
    Options +Multiviews
</Directory>

... And then you're done! Now take a look at an example of it in action:

http://{yoursite}/page.php can now be accessed as http://{yoursite}/page or even http://{yoursite}/page/variable or even http://{yoursite}/page/variable/variable/variable/variable !

What apache is doing is looking for the closest file match for the path sent to it and calling that file. All that is required to make these URLs usable is a function within your framework to extract data from the URL. Here's a basic function I have used before which could be adapted to your needs, it extracts the variable in the URL at $position:

function getWebParam($position) {
    static $params;

    if (!$params) {
        $params = explode('/', $_SERVER['PATH_INFO']);        
    }

    return $params[$position];
}

You could adapt it to parse URLs however you like (you don't have to separate variables based on a /, page-variable.html can also route to page.php), as long as the target file is the closest matching file for the URL entered.

Of course, there are downsides to using multiviews and it goes against the philosophy of directing all requests through your index.php file - but it does answer your request for simplicity and not having to maintain a htaccess file and is good to know about/consider.




回答7:


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




回答8:


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




回答9:


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



来源:https://stackoverflow.com/questions/3842445/what-is-a-good-way-to-have-a-seo-url-system-built-up-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!