Adnan Ahmed
Adnan Ahmed

Reputation: 686

URL rewrite rule not working on localhost (apache)

This is my link: http://localhost/test/index.php

Now i want to change it to look like this: http://localhost/home/

So i create a .htaccess file and placed it inside the test folder.

This is my .htaccess code:

RewriteEngine on

RewriteRule ^test/index.php$ home/

mod_rewrite is also enabled.

But anyway this rule is not working for me and browser is redirecting me to the original url http://localhost/test/index.php. However i tried this rule online and it seemed working there. Here's the snapshot of the website where i tested the rule.

enter image description here

Upvotes: 2

Views: 7950

Answers (2)

Jatin Bansal
Jatin Bansal

Reputation: 873

Try these paths:

  1. In terminal run a2enmod rewrite and restart apache.

  2. Apache upto 2.3

If it was done , Do the following,

go to this directory,

cd /etc/apache2/sites-available/

sudo gedit default

Replaces this line :

Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all and change it as below

Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all Then start apache server

sudo service apache2 restart

Apache 2.4+

sudo gedit /etc/apache2/apache2.conf

Add following in the end

```

Removes error that comes up while restarting server

ServerName localhost

phpmyadmin 404 error resolved

Include /etc/phpmyadmin/apache.conf ```

Also set AllowOverride All for /var/www Directory

Then follow these steps

sudo adduser <username> www-data sudo chown -R www-data:www-data /var/www sudo chmod -R g+rw /var/www

Restart apache

Done!!!

Check /var/log/apache2/error.log if still facing problems

Upvotes: 1

anubhava
anubhava

Reputation: 784958

You need to have 2 complimentary rules:

RewriteEngine on
RewriteBase /

# external redirect for chancing URL
RewriteCond %{THE_REQUEST} \s/+test/index\.php[\s?] [NC]
RewriteRule ^ home/ [L,R=302]

# internal routing to actual URL
RewriteRule ^home/?$ test/index.php [L,NC]

Upvotes: 0

Related Questions