Mod Rewrite is giving me a hard time

╄→гoц情女王★ 提交于 2019-12-24 06:44:30

问题


I'm currently using wamp on windows 7. I'd like to clean my urls obviously. I've tried to find the current syntax and what not, but I haven't figured it out.

My path right now is localhost/rs/index.php
When I go to localhost/rs/user it gives me a 404, but localhost/rs/ gives me the index.php page.

This is what I have in my .htaccess file at the www directory of wamp.

RewriteEngine On
RewriteRule ^/$ index.php
RewriteRule ^/([a-z]+)$ index.php?page=$1
RewriteRule ^/([a-z]+)/$ index.php?page=$1

I have un-commented the line

LoadModule rewrite_module modules/mod_rewrite.so

in the httpd.conf file of Apache

What's wrong? Is my .htaccess file in the wrong spot? Is my syntax wrong?

Thanks!


回答1:


Place your .htaccess in /rs folder and try

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]*)/?$ index.php?page=$1 [L]

Hope this will help




回答2:


Your .htaccess file should be placed in '/rs' folder, in the same directory where index.php is.

I've tried with enabled rewrite log, and what I saw there, when tried to access localhost/test/user:

::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user

::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/$' to uri 'user'

::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user ::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/([a-z]+)$' to uri 'user'

::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user

::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/([a-z]+)/$' to uri 'user'

::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (1) [perdir D:/Development/htdocs/test/] pass through D:/Development/htdocs/test/user

From the first line it is clear, that mod_rewrite is stripping beginning '/', and you are getting 'user' instead of '/user'. So, rewrite rules should be written without '/', that is:

RewriteEngine On
RewriteRule ^$ index.php
RewriteRule ^([a-z]+)/?$ index.php?page=$1

Also notice, that I've combined two last rules by writing '/?'. That means '/' symbol at the end of url is optional.

In order to turn on rewrite log, set the following in your httd.conf file:

#
# Logging for mod_rewrite
# Use RewriteLogLevel 3 only for debug purposes
# Normally use RewriteLogLevel 0
#
<IfModule rewrite_module>
    RewriteLogLevel 3
    RewriteLog "logs/rewrite.log"
</IfModule>

That way log will be created in logs/rewrite.log file. And this is usually the best way to examine what goes wrong.




回答3:


If you're running it in subfolder you need to add this line after RewriteEngine On

RewriteBase /rs

Also, make sure that in your apache virtual host section has this value

AllowOverride All


来源:https://stackoverflow.com/questions/12852962/mod-rewrite-is-giving-me-a-hard-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!