简体   繁体   English

HTaccess 404与mod_rewrite

[英]HTaccess 404 with mod_rewrite

I've got a small problem with my HTaccess urls. 我的HTaccess网址有一个小问题。 I wanted to make my URLs more SEO friendly, my current URL is: 我想让我的网址更加友好,我目前的网址是:

url.com/blogitem/2 url.com/blogitem/2

url.com -> Domain ; url.com - >域名; blog -> blogitem.php ; 博客 - > blogitem.php; 2 -> ID for the blog item to get from the DB 2 - >从DB获取博客项目的ID

Now I would like the URL to be like this: 现在我希望URL如下:

url.com/blogitem/2/name-title-info url.com/blogitem/2/name-title-info

I've made the HTaccess part and the PHP part for it, but it will give me a 404 error page in return every time I visited the URL. 我已经为它创建了HTaccess部分和PHP部分,但每次访问URL时它都会给我一个404错误页面。

The HTaccess part: HTaccess部分:

# Blog SEO Urls.
RewriteRule ^blog/([0-9]*)/?$        blog.php?page=$1    [NC,L]
RewriteRule ^blogitem/([0-9]*)/?$/([a-z]*)/?$    blogitem.php?id=$1&name=$2    [NC,L]

The PHP part: PHP部分:

$trancTitle = trim(strtolower($blogTitle));
$underscoreTitle = preg_replace('/\s+/', '-', $trancTitle);
<a href="blogitem/<?php echo $blogId; ?>/<?php echo $underscoreTitle;?>">

Can someone explain why I'm getting an error page in return(404 to be exactly). 有人可以解释为什么我得到一个错误页面作为回报(准确地说是404)。 I'm sorry for my bad English, it isn't my mother tongue. 对不起我的英语不好,这不是我的母语。 Thanks for helping in advance! 感谢您提前帮助!

Your regex doesn't seem right, try these 2 rules: 你的正则表达式似乎不正确,试试这两个规则:

RewriteEngine On
RewriteBase /

# Blog SEO Urls.
RewriteRule ^blog/([0-9]+)/?$ blog.php?page=$1 [NC,L,QSA]
RewriteRule ^blogitem/([0-9]+)/([\w-]+)/?$ blogitem.php?id=$1&name=$2 [QSA,NC,L]

The url that you are showing does not have "/" at the end: url.com/blogitem/2/name-title-info Then you have mentioned blog.php in your .htaccess I think you are wanting to have this url too url.com/blog/2 您显示的网址末尾没有“/”:url.com/blogitem/2/name-title-info然后您在.htaccess中提到了blog.php我觉得您也想要这个网址url.com/blog/2

So, you need to change a little bit you regular expression, try to putting this in your .htaccess: 所以,你需要改变一下你的正则表达式,试着把它放在你的.htaccess中:

# Blog SEO Urls.
RewriteRule ^blog/([0-9]+)$ blog.php?page=$1 [L]
RewriteRule ^blogitem/([0-9]+)/([a-z]+)$ /blogitem.php?id=$1&name=$2 [L]

Then in your php you will receive the variables via GET method Try this to see that: 然后在您的PHP中,您将通过GET方法接收变量尝试此操作以查看:

<?php
    echo '<pre>';
    print_r($_GET);
    echo '</pre>';
?>

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

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