我正在尝试为动态创建的页面创建"漂亮的url "。我的愿望是呈现索引页中的所有页面。
我在我的电脑上使用wamp 2.5/apache 2.4.9
apache httpd.conf
设置为:
Listen 0.0.0.0:7080
Listen [::0]:7080
DocumentRoot "c:/wamp/www/"
ServerName localhost:7080
我尝试了几种不同的方法,导致从空白页面返回到404错误页面的一切。下面的代码返回到wamp中/www/
文件夹中的index.php
。
这是我的无效.htaccess
代码:
# Turn Rewrite Engine
Options +FollowSymLinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
# Pages
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
# Rewrite to www.
RewriteCond %{HTTP_HOST} ^localhost:7080/demo [nc]
RewriteRule ^(.*)$ http://www.localhost:7080/demo/$1 [r=301,nc]
我认为最重要的一行代码是这样的:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
我认为是某种路径问题,但我不确定…如有任何帮助,不胜感激。
地址中的URL为:http://localhost:7080/demo/test
,其中demo
是wamp/www/
中的站点文件夹
http://localhost:7080/demo/test/
(带正斜杠)返回404错误
-
您的htaccess缺乏重写基础。RewriteEngine on
添加writebase/demo/
-
严格不允许尾随斜杠RewriteRule ^([a-zA-Z0-9]+)$ index.php?主题= 1美元与代码:
rewritecsecond %{REQUEST_URI} !-f
rewriterrule ^([a- za -z -9]+)$ index.php?用户名= 1美元(N, L)
这将允许以斜杠结尾的url强制返回到您的重定向url。
-
从最后一行删除:
http://www.localhost: 7080。/demo/$1 [r=301,nc]
最后一行的第二行没有做很多事情。你可以这样做,我建议你把它扔掉。
请记住,当离开本地主机环境到在线主机时,将所有出现的"/demo/"替换为"/"
你的最终htaccess应该是这样的:
# Turn Rewrite Engine
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
#RewriteCond %{REQUEST_FILENAME} !-d
# Pages
#RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]
# Rewrite to www.
RewriteRule ^(.*)$ /demo/$1 [r=301,nc]
希望对大家有所帮助
在/demo/.htaccess
中试试这些规则:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/
# Rewrite to www.
#RewriteCond %{HTTP_HOST} ^localhost$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}:7080%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?topic=$1 [L,QSA]