If I have a dynamic page, where it takes in an id parameter like example.com/posts.php?id=2, how do I make a RewriteRule in htaccess so that the url shows the title of the post rather than its id, so for example posts.php?id=2 shows a post with a title of "PHP is cool", I want the url to be rewritten like example.com/2/php-is-cool or something like that? Would that be possible if the title value is stored in a MySQL database?
如果我有一个动态页面,它带有一个id参数,例如example.com/posts.php?id=2,如何在htaccess中创建一个RewriteRule,以便url显示帖子的标题而不是id,所以例如posts.php?id = 2显示标题为“PHP很酷”的帖子,我想要重写网址,例如example.com/2/php-is-cool或类似的东西?如果标题值存储在MySQL数据库中,那会是可能的吗?
Additional Info:
This is how my htaccess looks like:
这就是我的htaccess的样子:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
ErrorDocument 404 /index.php
ErrorDocument 403 /index.php
php_value post_max_size 20M
Basically, I have a MySQL database which stores blog posts in a table called posts. The posts table has an id attribute (which is the auto-increment primary key), a title attribute, and a content attribute. When I access mydomain.com/posts.php?id=X, it will show my post with an id of 'X' from the database on the webpage. I just want to be able to re-write the URL such that it shows the title of the page. I'm doing this primarily for SEO, not aesthetics. Is this possible using htaccess, or would I have to approach this differently?
基本上,我有一个MySQL数据库,它将博客帖子存储在一个名为posts的表中。 posts表具有id属性(自动增量主键),title属性和content属性。当我访问mydomain.com/posts.php?id=X时,它会在网页上的数据库中显示我的帖子,其ID为“X”。我只是希望能够重新编写URL,以便显示页面的标题。我这样做主要是为了SEO,而不是美学。这可能是使用htaccess,还是我必须采用不同的方法?
2 个解决方案
#1
2
Rewrite rules apply to the way the URL is handled on your backend (apache). Using mod_rewrite to rewrite those URLs will not affect what the user is seeing in their browser's URL bar unless you redirect the user to a new URL (using a 302 redirect, for example), which would cause the browser to reload the page.
重写规则适用于在后端(apache)处理URL的方式。使用mod_rewrite重写这些URL不会影响用户在浏览器的URL栏中看到的内容,除非您将用户重定向到新的URL(例如,使用302重定向),这将导致浏览器重新加载页面。
You can achieve what you're asking for using the HTML5 pushstate feature (with a fallback to URL hashes if pushstate is not supported in that browser).
您可以实现使用HTML5 pushstate功能所要求的内容(如果该浏览器不支持pushstate,则会回退到URL哈希)。
Take a look at this URL for more details:
请查看此URL以获取更多详细信息:
http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate
Also, you could use BackboneJS and its Router feature to handle your page and URL handling logic in the browser.
此外,您可以使用BackboneJS及其路由器功能来处理浏览器中的页面和URL处理逻辑。
This is a very application specific kind of solution and this answer cannot go into more details without knowing your exact configuration, application logic, etc.
这是一种非常特定于应用程序的解决方案,如果不知道您的确切配置,应用程序逻辑等,这个答案就无法详细说明。
#2
0
If you're doing it for SEO only, the easiest way is to pass all URLs to one single script which analyses the request and delivers the content from the database. Like in
如果您只是为SEO做,最简单的方法是将所有URL传递给一个单独的脚本,该脚本分析请求并从数据库传递内容。像
RewriteRule (.*) index.php?url=$1
Of course you will have to exclude index.php from the rule.
当然,您必须从规则中排除index.php。
#1
2
Rewrite rules apply to the way the URL is handled on your backend (apache). Using mod_rewrite to rewrite those URLs will not affect what the user is seeing in their browser's URL bar unless you redirect the user to a new URL (using a 302 redirect, for example), which would cause the browser to reload the page.
重写规则适用于在后端(apache)处理URL的方式。使用mod_rewrite重写这些URL不会影响用户在浏览器的URL栏中看到的内容,除非您将用户重定向到新的URL(例如,使用302重定向),这将导致浏览器重新加载页面。
You can achieve what you're asking for using the HTML5 pushstate feature (with a fallback to URL hashes if pushstate is not supported in that browser).
您可以实现使用HTML5 pushstate功能所要求的内容(如果该浏览器不支持pushstate,则会回退到URL哈希)。
Take a look at this URL for more details:
请查看此URL以获取更多详细信息:
http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate
Also, you could use BackboneJS and its Router feature to handle your page and URL handling logic in the browser.
此外,您可以使用BackboneJS及其路由器功能来处理浏览器中的页面和URL处理逻辑。
This is a very application specific kind of solution and this answer cannot go into more details without knowing your exact configuration, application logic, etc.
这是一种非常特定于应用程序的解决方案,如果不知道您的确切配置,应用程序逻辑等,这个答案就无法详细说明。
#2
0
If you're doing it for SEO only, the easiest way is to pass all URLs to one single script which analyses the request and delivers the content from the database. Like in
如果您只是为SEO做,最简单的方法是将所有URL传递给一个单独的脚本,该脚本分析请求并从数据库传递内容。像
RewriteRule (.*) index.php?url=$1
Of course you will have to exclude index.php from the rule.
当然,您必须从规则中排除index.php。