-1

我想要以下网址:

http://localhost/new/post?url=sample-post-one

通过 htaccess mod_rewrite 看起来像这样:

http://localhost/new/post/sample-post-one/

这可能是一个已经问过的问题或类似的问题,但几个小时以来我一直在尝试解决这个问题,但没有找到解决方案。

任何帮助将不胜感激。谢谢!

更新 这是我尝试过的

<Files .htaccess,.svn> order allow,deny deny from all </Files>
Options +FollowSymlinks
# For Removing .php extension from files
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# Rewrite rule
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]
4

3 回答 3

0

基本上我们正在捕获最后一个文件夹的位。其余的可以硬编码。此外,您应该删除竞争规则。^表示字符串的开头。$是字符串的结尾。如果您同时使用这两个字符,则除了我们使用REGEX模式进行匹配的位之外,所有内容都必须逐字匹配。一切都区分大小写。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^/]+)/?$ new/post?url=$1

如果您知道您只允许使用字母、数字、句点和连字符,那么您可以这样做:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^new/post/([^A-Za-z0-9.-]+)/?$ new/post?url=$1

如果由于某种原因您包含new/post并且post确实是您的文档根目录,它应该如下所示:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^A-Za-z0-9.-]+)/?$ ?url=$1

Apache 是一个有据可查的应用程序。他们在其站点上提供了具有数百个示例的优秀资源,并在mod_rewrite 文档中对每个示例进行了解释。

于 2013-06-17T14:44:38.733 回答
0

您可以使用以下代码:

http://localhost/new/post?url=sample-post-one
RewriteRule    ^post/([0-9]+)/?$    post?url=$1    [NC,L]    # Handle  post requests

要获取更多信息,您可以参考此

于 2013-06-17T14:38:48.497 回答
0

这应该工作:

RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post?url=$1 [L]

或更好 ...

RewriteEngine on
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ post?url=$1 [L]

顺便说一句,你应该看看像这样的指南。

于 2013-06-17T14:37:49.527 回答