Apache rewrite - clean URLs in localhost

时间秒杀一切 提交于 2020-01-04 03:12:06

问题


I have been trying for the past few hours to write clean URLs in my LAMP machine.

Apache's mod_rewrite is enabled, and I am trying to use an .htaccess file to pass the URL GET parameters to my index.php script which at the time is just a var_dump of _GET.

My current .htaccess is the following (although I've tried quite a few variations of it I found on other answers with no success)

RewriteEngine On

#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f

#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d 

#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?/get=$1 [L]

When I point my browser to localhost/my_project/test, all I get is a 404 error saying:

Not Found

The requested URL /my_project/test was not found on this server.

Apache/2.2.22 (Ubuntu) Server at localhost Port 80

Apparently I'm missing something obvious here, any help?


回答1:


2 things to look for:

  1. Make sure .htaccess is enabled
  2. Make sure above code is in my_project directory.

Then add RewriteBase line as well:

RewriteEngine On
RewriteBase /my_project/

#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d 
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?get=$1 [L]


来源:https://stackoverflow.com/questions/19211824/apache-rewrite-clean-urls-in-localhost

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