2

我的网址:http://localhost/test.php

我在用:

.ht 访问:

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

PHP:

$url = $_GET['url'];
echo var_dump($url);

但我为 $url 得到的只是:NULL NULL NULL NULL NULL NULL

4

2 回答 2

6

编辑:调整为处理重定向和重写。

RewriteEngine On 
RewriteBase /

# Redirect .php URLs to rewritten URLs
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)\.php$ $1 [L,QSA,R=301]

# Rewrite URLs for processing by router (index.php)
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NC]

您应该排除该RewriteCond %{REQUEST_FILENAME} !-d条件,因为如果您的 URL 匹配它,它将尝试访问目录。在进行 url 重写时可能是不可取的。

索引.php

$url = isset($_GET['url']) ? $_GET['url'] : null;
var_dump($url);
于 2013-05-15T01:42:01.903 回答
2

我只是想把它留给未来的用户。这将从 URLS 中删除 HTML 和 PHP 扩展。

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
于 2013-09-06T22:00:35.050 回答