重写 URL 在本地主机上不起作用
Rewrite URL is not working from localhost
我一开始尝试用 .htaccess 隐藏我的 .php 扩展名。将它包含在文件中后效果很好
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php
并将其包含在我的 function.php
中
//==== Strip .php extension from requested URI
function strip_php_extension()
{
$uri = $_SERVER['REQUEST_URI'];
$ext = substr(strrchr($uri, '.'), 1);
if ($ext == 'php')
{
$url = substr($uri, 0, strrpos($uri, '.'));
redirect($url);
}
}
//==== Redirect. Try PHP header redirect, then Java, then http redirect
function redirect($url)
{
if (!headers_sent())
{
/* If headers not yet sent => do php redirect */
header('Location: '.$url);
exit;
}
else
{
/* If headers already sent => do java redirect */
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
/* If java is disabled => do html redirect */
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0; url='.$url.'" />';
echo '</noscript>';
exit;
}
}
动态页面的 url 没有改变..我还有 page.php?p=5
和 blog.php?post=5 不更改页面?p=5 和博客?post=5..
我确实想将上面的动态页面 url 更改为 www.yourdomian.com/5 用于 page.php 和 www.yourdomain.com/blog/5 用于博客,但这不是从我在 htacces
中的 ReWrite 规则中读取的
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?p=
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?p=
请指出我做错了什么的正确方向。以及如何修复它。
您可以在 /MyApp/.htaccess
中使用这些规则:
Options -MultiViews
RewriteEngine On
RewriteBase /MyApp/
RewriteCond %{THE_REQUEST} /page\.php\?([a-z])=([^&\s]+)
RewriteRule ^ %1/%2? [R=302,NE,L]
RewriteCond %{THE_REQUEST} /blog\.php\?post=([^&\s]+) [NC]
RewriteRule ^ blog/%1? [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ .php [L]
RewriteRule ^blog/([^/]+)/?$ blog.php?post= [L,NC,QSA]
RewriteRule ^([a-z])/([^/.]+)/?$ page.php?= [L,QSA]
我一开始尝试用 .htaccess 隐藏我的 .php 扩展名。将它包含在文件中后效果很好
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php
并将其包含在我的 function.php
中//==== Strip .php extension from requested URI
function strip_php_extension()
{
$uri = $_SERVER['REQUEST_URI'];
$ext = substr(strrchr($uri, '.'), 1);
if ($ext == 'php')
{
$url = substr($uri, 0, strrpos($uri, '.'));
redirect($url);
}
}
//==== Redirect. Try PHP header redirect, then Java, then http redirect
function redirect($url)
{
if (!headers_sent())
{
/* If headers not yet sent => do php redirect */
header('Location: '.$url);
exit;
}
else
{
/* If headers already sent => do java redirect */
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
/* If java is disabled => do html redirect */
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0; url='.$url.'" />';
echo '</noscript>';
exit;
}
}
动态页面的 url 没有改变..我还有 page.php?p=5 和 blog.php?post=5 不更改页面?p=5 和博客?post=5..
我确实想将上面的动态页面 url 更改为 www.yourdomian.com/5 用于 page.php 和 www.yourdomain.com/blog/5 用于博客,但这不是从我在 htacces
中的 ReWrite 规则中读取的RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?p=
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?p=
请指出我做错了什么的正确方向。以及如何修复它。
您可以在 /MyApp/.htaccess
中使用这些规则:
Options -MultiViews
RewriteEngine On
RewriteBase /MyApp/
RewriteCond %{THE_REQUEST} /page\.php\?([a-z])=([^&\s]+)
RewriteRule ^ %1/%2? [R=302,NE,L]
RewriteCond %{THE_REQUEST} /blog\.php\?post=([^&\s]+) [NC]
RewriteRule ^ blog/%1? [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ .php [L]
RewriteRule ^blog/([^/]+)/?$ blog.php?post= [L,NC,QSA]
RewriteRule ^([a-z])/([^/.]+)/?$ page.php?= [L,QSA]