Skip to content

Instantly share code, notes, and snippets.

@bramus
Last active November 10, 2023 20:20
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bramus/5332525 to your computer and use it in GitHub Desktop.
URL Rewriting for Apache (requires mod_rewrite) and IIS (requires IIS url rewrite module)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
@yunusga
Copy link

yunusga commented Apr 12, 2015

NGINX

location / {
        try_files $uri /index.php;
    }

@ifeiwu
Copy link

ifeiwu commented Dec 13, 2016

nginx.conf

location / {
	if (!-e $request_filename) {
		rewrite ^/(.*)$ /index.php?$1 last;
	}
}

@RedShift1
Copy link

RedShift1 commented Aug 24, 2017

For some reason the .htaccess sample doesn't work if you put it in a vhost configuration.

Update: see http://tltech.com/info/rewriterule-in-htaccess-vs-httpd-conf/

@ifeiwu
Copy link

ifeiwu commented Jan 17, 2018

location / {
	try_files $uri $uri/ /index.php?$args;
}

@TomGranot
Copy link

TomGranot commented Oct 5, 2019

To follow up on @RedShift1's answer from above, if you'd like to make this work in a virtual host configuration in apache you don't need to put the .htaccess file in the project's root folder.
This is my sites-available/<my-domain>.conf:

<VirtualHost *:80>
  ServerName <my-domain>
  # You don't have to put an alias in, but it's useful
  ServerAlias www.<my-domain>
  DocumentRoot /var/www/<my-domain>/public_html/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  # Note that this is in a different context now - not in the virtual host, but in the actual root directory.
  <Directory /var/www/<my-domain>/public_html/>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
  </Directory>
</VirtualHost>

@kmvan
Copy link

kmvan commented Mar 3, 2020

try_files $uri $uri/ /index.php?$args;

Works man.

@kmvan
Copy link

kmvan commented Mar 3, 2020

NGINX

location / {
        try_files $uri /index.php;
    }

That will lost $_GET contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment