简体   繁体   English

URL中带有.htaccess和Php的页面标题

[英]Page Title in URL with .htaccess and Php

I have been smashing my head against wall for whole day but could not understand how can I make my URL show the page title in URL instead of all query parameters. 我整天都在砸墙,但无法理解如何使我的URL显示URL中的页面标题而不是所有查询参数。

I just simply wants to convert my this URL 我只是想转换这个网址

http://www.def.com/post.php?id=1 http://www.def.com/post.php?id=1

to

http://www.def.com/my-page-title.html http://www.def.com/my-page-title.html

I read many articles and specially questions on SO but non of them were really answering this. 我读了许多文章,特别是关于SO的问题,但没有一个是真正的答案。 I do not have enough reputations to put other similar links here. 我没有足够的声誉在这里放置其他类似的链接。

I just want to get the title of my page and show it in my URL. 我只想获取页面标题并将其显示在URL中。

A fairly standard .htaccess that will route all paths where a directory or file are not found at the path: 一个相当标准的.htaccess文件,它将路由所有在目录中找不到目录或文件的路径:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^route-page\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /route-page.php [L]
</IfModule>

Thereafter, you have to have the /route-page.php (whatever you name it) read the URI: $_SERVER['REQUEST_URI'], parse "/my-page-title.html" and figure out how to load the correct page from there. 此后,您必须让/route-page.php(无论您使用什么名称)读取URI:$ _SERVER ['REQUEST_URI'],解析“ /my-page-title.html”并找出如何加载正确的从那里开始。

route-page.php 路由page.php文件

<?php
    $path = $_SERVER['REQUEST_URI'];
    //query for row of data with that path...
    //output results

Because your actual page name may have symbols in it, it's best if you have the expected path "my-page-title.html" in an indexed column of your database. 因为您的实际页面名称中可能包含符号,所以最好在数据库的索引列中具有预期的路径“ my-page-title.html”。 This way, you can quickly grab the page based on the path. 这样,您可以根据路径快速抓取页面。 Also, since all unfound URLs are going to this page, you need to handle 404 errors manually (ie if you don't find a page that matches the path specified, output a 404 error): 另外,由于所有未找到的URL都将转到此页面,因此您需要手动处理404错误(即,如果找不到与指定路径匹配的页面,则输出404错误):

if( $pageNotFound ) {
    header("HTTP/1.0 404 Not Found");
    echo("<h1>Page Not Found</h1>");
}

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

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