Ollie
Ollie

Reputation: 1435

.htaccess RewriteRule doesn't do anything

Apologies if an answer for this already exists, I have looked but can't find anyone with the same problem. mod_rewrite is enabled on my localhost but the rewrite rules I have just don't seem to work.

Options -indexes

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /Website

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Rewrite index.php
    RewriteRule ^Website([^/]*)/$ /Website/?action=$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

I even used this mod rewrite generator but the generated rules don't work either. Stuff I install locally like Wordpress manage to rewrite the URLs fine, so there must be something wrong with the code I've written

I am out of my depth with this .htaccess stuff so I really appreciate any answers I can get.

Thanks.

Upvotes: 0

Views: 133

Answers (2)

anubhava
anubhava

Reputation: 785226

Try this code:

Options -indexes

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /Website

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Rewrite index.php
    RewriteRule ^([^/]+)/?$ ?action=$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

UPDATE:

RewriteEngine On
RewriteBase /Website/
RewriteRule ^([^/.]+)/?$ ?action=$1 [L,QSA]

Upvotes: 0

Michael Coxon
Michael Coxon

Reputation: 5510

Once you have set the RewriteBase you do not need to redeclare it in the RewriteRule

Your rule should read...

RewriteRule ^([^/]*)/$ /Website/?action=$1 [L]

That'll do it! (this is taking into account that your regex is also fine :p)

EDIT: (explanation)

Once you have set the RewriteBase mod_rewrite will only look at everything AFTER the base, so what RewriteRule was absolutely looking for was /Website/Website([^/]*)/.

Upvotes: 1

Related Questions