简体   繁体   English

PHP自动创建seo友好的URL

[英]PHP auto create seo friendly url

I have PHP url for show content for each page in language like : 我有PHP网址,用于显示每个页面的节目内容,如:

category: 类别:

articles
gallery
page
books
....

URL: 网址:

http://localhost/cms/load.php?page=articles&id=1&title=this-is-a-title.html&lang=ru
http://localhost/cms/load.php?page=page&id=10&title=this-is-a-page-title.html&lang=ru

In action i need to auto rewrite url to seo friendly url like this : 在行动中我需要像这样自动重写url到seo友好的url:

http://localhost/cms/ru/articles/1/this-is-a-title.html
http://localhost/cms/ru/page/10/this-is-a-page-title.html

load.php load.php

$page = isset($_GET['page']) ? safeGET($_GET['page']) : null;
$id   = isset($_GET['id'])   ? safeGET($_GET['id'])   : null;
if ($page=='articles') { include 'article.php'; } elseif { ... }

how do can i generate this URL with PHP (Preference without mod-rewrite)?! 我怎样才能用PHP生成这个URL (没有mod重写的偏好)?! OR with .htaccess and PHP ? 或者.htaccess and PHP

You can achieve this in the following way: 您可以通过以下方式实现此目的:

.htaccess 的.htaccess

RewriteEngine on
RewriteBase /cms/
RewriteRule ^([a-z,A-Z]+)/([a-z,A-Z]+)/([0-9]+)/([a-z,A-Z,0-9]+)$ ./load.php?page=$2&id=$3&title=$4&lang=$1 [L,NC]

This way your load.php file doesn't need to change and can accept the seo urls. 这样你的load.php文件不需要改变,可以接受seo urls。

You will need to change the code that creates your URLs, if you update your answer how your URL's are currently being generated we can help you with that too. 您需要更改创建网址的代码,如果您更新了当前生成网址的答案,我们也可以为您提供帮助。

I'd recommend using a routing class. 我建议使用路由类。 You'd add a htaccess file that rewrites everything to a index.php file, then you'd control all the routing within PHP. 您将添加一个htaccess文件,将所有内容重写为index.php文件,然后您将控制PHP中的所有路由。

AltoRouter is my favorite one https://github.com/dannyvankooten/AltoRouter . AltoRouter是我最喜欢的一个https://github.com/dannyvankooten/AltoRouter It's inspired by Ruby's Sinatra, but if you Google "PHP routing class" you can find many more similar classes to use for routing your urls. 它的灵感来自Ruby的Sinatra,但如果您使用Google“PHP路由类”,您可以找到更多类似的类来用于路由您的URL。

save yourself some pain and use a routing framework (ie http://www.slimframework.com/ ) 为自己节省一些痛苦并使用路由框架(即http://www.slimframework.com/

though creation of the seo friendly urls (also called slugs) is not included 虽然不包括创建 seo friendly urls (也称为slugs)

Here you can find two useful guides i used to understand how .htacces works. 在这里,您可以找到两个有用的指南,用于了解.htacces的工作原理。 There are a lot of good example, I hope it helps: 有很多很好的例子,我希望它有所帮助:

First guide: http://code.tutsplus.com/tutorials/the-ultimate-guide-to-htaccess-files--net-4757 第一个指南: http //code.tutsplus.com/tutorials/the-ultimate-guide-to-htaccess-files--net-4757

Second guide: https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/ 第二个指南: https //www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM