13030

Apache rewrite - clean URLs in localhost

Question:

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:

<blockquote>

Not Found

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

Apache/2.2.22 (Ubuntu) Server at localhost Port 80

</blockquote>

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

Answer1:

2 things to look for:

<ol><li>Make sure .htaccess is enabled</li> <li>Make sure above code is in my_project directory.</li> </ol>

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]

Recommend

  • SimpleXML - echo / print_r return different values
  • file_get_Contents won't send query strings in url , thus receiving nothing with $_GET
  • .htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact
  • Smart URL with optional parameters?
  • Removing the .php file extension from the URL Request
  • Why am I getting an Array of an Array with Doctrine 2 & Symfony 2 Data Transformer?
  • Index.php as custom error page
  • PHP file_exists() anomaly
  • Typecasting `this` of a base class template to its derived class
  • testing a POST using phpunit in laravel 4
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • How do I mock an exported typescript function in a jasmine test?
  • SyntaxError: (irb):26: both block arg and actual block given
  • Salesforce Different WSDL files and when to use
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Time complexity of a program which involves multiple variables
  • Checking free space on FTP server
  • How to install a .deb file on a jailbroken iphone programmatically?
  • DomPDF {PAGE_NUM} not on first page
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Javascript simulate pressing enter in input box
  • PHP - How to update data to MySQL when click a radio button
  • Does CUDA 5 support STL or THRUST inside the device code?
  • htaccess rewriting URLs with multiple forward slashes
  • Importing jscolor library in angular 2
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • PHP: When would you need the self:: keyword?
  • A cron job substitute?
  • json Serialization in asp
  • Proper way to use connect-multiparty with express.js?
  • Acquiring multiple attributes from .xml file in c#
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • reshape alternating columns in less time and using less memory
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?