.htaccess将mod_rewrite重定向返回到wamp

时间:2015-07-16 20:43:00

标签: php apache .htaccess mod-rewrite redirect

我正在尝试为动态创建的页面创建"漂亮的网址。我的愿望是从索引页面渲染所有页面。

我使用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中index.php文件夹中的/www/

这是我的非工作.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

我认为这是某种路径问题,但我不确定......任何协助都会受到赞赏。

地址中的网址为:http://localhost:7080/demo/test其中demowamp/www/

中的网站文件夹

http://localhost:7080/demo/test/(使用正斜杠)返回404错误。

2 个答案:

答案 0 :(得分:1)

  1. 您的htaccess缺少重写基础。 紧随其后:重写引擎

    添加RewriteBase / demo /

  2. 严格禁止使用尾部斜杠 替换此行:RewriteRule ^([a-zA-Z0-9] +)$ index.php?topic = $ 1 用这段代码:

    RewriteCond%{REQUEST_URI}!-f

    RewriteRule ^([a-zA-Z0-9] +)$ index.php?username = $ 1 [N,L]

  3. 这将允许结束尾部斜杠的网址强制返回您的重定向网址。

    1. 从最后一行删除:

      http://www.localhost:7080。 保留为:/ demo / $ 1 [r = 301,nc]

    2. 第二行到最后一行并没有做太多。你可以这样做,我建议你不要这样做。

      请记住,当离开本地主机环境以获取在线主机时,请替换所有" / 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]
      

      我希望这会有所帮助

答案 1 :(得分:0)

/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]