8 apache .htaccess mod-rewrite
我不是htaccess文件的专家,我正在尝试看似非常简单的东西,但我无法做到.
我搜索并最终发现这里的东西可以用于我的使用,但事实并非如此.代码如下.
这是我需要的一个例子:
http://localhost/Test/TestScript.php显示这个:http://localhost/Test/TestScript/脚本在原始位置.
这些是我的规则(复制):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /Test
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
Run Code Online (Sandbox Code Playgroud)
这条规则给出:
Forbidden You don't have permission to access ".../Test/TestScript.php on this server."
所以改为最后一行:
RewriteRule ^.* http://localhost/Test/TestScript.php [L]
Run Code Online (Sandbox Code Playgroud)
但现在得到这个错误:
The page isn't redirecting properly
希望这个地方有才华的人可以帮助我.谢谢.
规则集以循环结束.让我们来看看:
请求被http://localhost/Test/TestScript.php重定向到http://localhost/Test/TestScript/,以便浏览器显示它,最后尝试映射回原始资源.
许多人认为,规则中的[L]标志不会停止该过程.重写引擎循环遍历完整的规则集,逐个规则,当特定规则匹配时,它循环通过相应的条件(如果有的话).由于对每个请求重复此过程并且规则生成新请求,因此很容易进入无限循环.
这就是" 页面没有正确重定向 " 的消息,在这种情况下意味着什么.
以下是此过程的技术细节
I)最好和更实用的是在初始请求中直接使用"漂亮"URL,将其静默映射到资源.这是一个一步过程,其中"漂亮"URL始终显示在浏览器的地址栏中.此选项的一个优点是,传入URL的URI路径中不存在任何内容.
http://localhost/Test/TestScript/http://localhost/Test/TestScript.phpOptions +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Prevent loops
RewriteCond %{REQUEST_URI} !\.php [NC]
# Map internally to the resource, showing the "pretty" URL in the address bar
RewriteRule ^([^/]+)/([^/]+)/? /$1/$2.php [L,NC]
Run Code Online (Sandbox Code Playgroud)
II)如果那是不可能的,因为已经存在直接指向资源的链接,显示"漂亮"URL但仍然从原始请求获取数据的一种方法是首先进行可见且永久的重定向,将扩展名剥离到显示"漂亮"的URL,然后内部重写回原始资源.
http://localhost/Test/TestScript.php,http://localhost/Test/TestScript/"漂亮"的URL,http://localhost/Test/TestScript.php,原始请求.Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/([^/]+)/([^.]+)\.php [NC]
# Strip the extension and redirect permanently
RewriteRule .* /%2/%3/ [R=301,L,NC]
# Now the browser bar shows `http://localhost/Test/TestScript/`
# Prevent loops
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php [NC]
# Map internally to the original resource
RewriteRule ^([^/]+)/([^/]+)/? /$1/$2.php [L,NC]
Run Code Online (Sandbox Code Playgroud)
笔记:
mod_rewrite已启用.| 归档时间: |
|
| 查看次数: |
9089 次 |
| 最近记录: |