问题描述
我正在做我个人的CMS。我想在这凉爽(型)的URL来使用。这是我的.htaccess文件code:
I'm making my personal CMS. I want to use in it cool (friendly) URLs. This is my .htaccess file code :
RewriteEngine on
RewriteBase /
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z-_0-9_/]+)/?$ index.php?args=$1 [L]
我看到,堆栈溢出
也使用友好的URL。今天,我想对堆栈溢出做出错误。要(例如)这个网址 http://stackoverflow.com/questions/5469955/htaccess-url-rewrite-if-file-not-exists
我加了的.php
。但我没有看到404错误。我们在一个神奇的方法论坛删除了我的PostScript和重定向我回到正确的网站网址。我的问题是:如何才能让他同样在我的CMS ?我试图改变这种(重写规则^([A-ZA-Z-_0-9 _ /] +)/ $的index.php?的args = $ 1 [L]
)行(重写规则^([A-ZA-Z-_0-9 _ /] +)/([A-ZA-Z-_0-9)$的index.php?的args = $ 1 [L]
),但是这是没有帮助的,然后我看到 500内部服务器错误
。
I see that on Stack Overflow
are also used friendly URLs. Today I tried to make error on Stack Overflow. To (for example) this URL http://stackoverflow.com/questions/5469955/htaccess-url-rewrite-if-file-not-exists
I added .php
. But I didn't seen 404 Error. Our forum in a magic method deleted my postscript and redirected me back to correct site URL. And my question is : How can I make he same in my CMS ? I tried to change this (RewriteRule ^([a-zA-Z-_0-9_/]+)/?$ index.php?args=$1 [L]
) line to (RewriteRule ^([a-zA-Z-_0-9_/]+)/([a-zA-Z-_0-9)$ index.php?args=$1 [L]
), but this isn't helpful and then I see 500 Internal Server Error
.
感谢您的帮助和建议,
PS
如果你需要更多的inforamtion关于我的问题,请在评论写
If you need more inforamtion about my issue please write in comment
推荐答案
如果你需要有像这样,那么你得有一些服务器端支持(例如PHP,ASP等),以及相同的行为。请考虑以下code片断:
If you need to have same behavior like SO then you got to have some server side support (e.g. PHP, ASP etc) as well. Consider following code snippet:
的.htaccess:
.htaccess:
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?id=$1&title=$2 [L,NC,QSA]
的index.php:
index.php:
<?php
$id = $_GET['id'];
$title = $_GET['title'];
$dbTitle = // get title from Database query using $id
...
if ($title != $dbTitle) {
// redirect with 301 to correct /<id>/<title> page
header ('HTTP/1.1 301 Moved Permanently');
header('Location: /' . $id . '/' . $dbTitle);
exit;
}
// rest of your script
?>
这篇关于如果的.htaccess URL不好做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!