1

I created a simple website and some of the page urls are having the pattern "*.php".

I want to make my url /home.php as /home. Can I do this in .htaccess file?

I am adding my code here

<files campus_config.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<files student.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<FilesMatch (variables|mysql)\.php>
order allow,deny  
deny from all
</FilesMatch>

RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

sample url: localhost/test/campus/login.php (test is a folder ,campus is my project folder ,.htaccess file is in my project folder)

Vishnu
  • 35
  • 8

2 Answers2

0

What you are looking for is called URL rewriting. This is normally done by application server usually Apache or IIS (on windows). how to do it in Apache. On windows it depends on version (6-8) of IIS used.

Matas Vaitkevicius
  • 49,230
  • 25
  • 212
  • 228
0

Place this line on in your /test/campus/.htaccess:

RewriteEngine on
RewriteBase /test/campus/

RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • I am going to post my code here , RewriteEngine on RewriteRule ^home\.php$ home [L] ,do i need to enable content negotaiation module in apache? – Vishnu May 03 '14 at 08:06
  • Hi Anubhava,thanks ,I tried it .But still it is giving me the error "The requested URL /home was not found on this server".Evenafter changing the url,it is looking for home instead of home.php – Vishnu May 04 '14 at 05:34
  • What URL are you entering in your browser and what is location of this .htaccess file? Are there more rules? – anubhava May 04 '14 at 05:35
  • I put .htaccess inside the project directory(present inside parent directory) .This is my entire code in .htaccess order allow,deny deny from all Allow from 127.0.0.1 order allow,deny deny from all Allow from 127.0.0.1 order allow,deny deny from all RewriteEngine on RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,L,NE] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC] RewriteRule ^(.+?)/?$ /$1.php [L] – Vishnu May 04 '14 at 05:43
  • It is impossible to understand any code from comments. Edit your question and provide it there. Also is `projects` a sub directory under DocumentRoot? – anubhava May 04 '14 at 05:48
  • Make sure above is placed in `/test/campus/.htaccess` and you send a URL of `http://domain.com/test/campus/home` – anubhava May 04 '14 at 06:17
  • Yes ,I have placed it in campus/.htaccess and sent a url of http://domain.com/test/campus/home .Confirmed both. – Vishnu May 04 '14 at 06:38
  • It should be directly placed in `/test/campus/.htaccess` – anubhava May 04 '14 at 06:39
  • Because other lines are not mod_rewrite rules and don't depend on base path of .htaccess – anubhava May 04 '14 at 06:49
  • I checked ,it is directly placed in /test/campus/.htaccess – Vishnu May 04 '14 at 06:55
  • So now you can open `http://localhost/test/campus/home.php` but `http://localhost/test/campus/home` gives 404? – anubhava May 04 '14 at 06:59
  • Yes anubhava , I tried to open http://localhost/test/campus/home.php ,it is redirected to http://localhost/test/campus/home and giving me 404 – Vishnu May 04 '14 at 07:05
  • Yes that redirection is as per first 301 rule and that worked fine. Not sure why 2nd one didn't work for you – anubhava May 04 '14 at 07:06
  • Can you try placing `Options -MultiViews` line on top of your .htaccess – anubhava May 04 '14 at 07:42
  • ok comment out both `RewriteCond` lines before last rule and retry – anubhava May 04 '14 at 07:53