我的网址:http://localhost/test.php
我正在使用:
htaccess的:
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
答案 0 :(得分: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
条件,因为如果您的网址与之匹配,它会尝试访问目录。在进行网址重写时可能不太理想。
的index.php
$url = isset($_GET['url']) ? $_GET['url'] : null;
var_dump($url);
答案 1 :(得分:2)
我只是想把这个留给未来的用户。这将从URL中删除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]