1

I am new in .htaccess file, so I want to change my every file different URL 1st default I use this its work for only 1 file

RewriteRule ^([a-zA-Z0-9-/]+).html$ xrl.php?xrl=$1

Then I change my contactus.php file URL then I do this from Google below

RewriteRule ^Contact-us$ contact.php [L] 

Its not work some method more I try but whole website not work. How I change my every file different URL as I want.

  • you want to remove .php extension from page name? – Zeeshan Mar 25 '14 at 05:37
  • @Zeeshan yeh remove .php but specific files. aboutus.php and contactus.php also i want to change url Like contactus.php as Contact-Us and aboutus.php as About-us – user3458209 Mar 25 '14 at 05:39

2 Answers2

0

Try this code for hiding .php

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

OR

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Reference Links

Link 1

Link 2

Link 3

Community
  • 1
  • 1
Zeeshan
  • 1,659
  • 13
  • 17
0

We can rewrite urls using .htaccess files. But before writing something in .htaccess files we need to verify which kind of server we are using. Here in the case you may use either an apache server or a Nginx server. You can easily check by just adding a

<?php phpinfo(); ?>

in a php page.

Case 1 : Apache Server:

Ensure that mod_rewrite module enabled in apache server. This can be identified by checking phpinfo included page.

Common methods used for url rewriting is as follows

RewriteEngine

This is mandatory for any apache server. This is used to enable the rewriting engine. In some cases it should be on by default.So ensure that following code must be mandatory in top of your htaccess file

RewriteEngine On

RewriteRule

in some cases you may have only few pages and you can specify each page in your website in .htaccess file. In such case you can use these kind of rewriting For example

RewriteRule ^article/used/to/be/here.php$ /article/now/lives/here/ [R=301,L]

Suppose you have to rewrite url like these

http://en.wikipedia.org/wiki/url_rewriting

You should write something like these on your .htaccess file

RewriteEngine On
RewriteRule   ^wiki/(.+)$   w/index.php?title=$1   [L]

But actual implementation of the page will be like these

http://en.wikipedia.org/w/index.php?title=url_rewriting

RewriteCond

This can be used for rewriting certain url with respect to some conditions. Suppose you need to add or remove www with your website name and on certain condition redirect to certain page like "url not available or error message "

Example :

RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC] 

You can further refer here

Introduction to url rewriting

Rewrite with examples

Case 2 : nginx Server

Enable module HttpRewriteModule on your nginx server Use location to rewrite you urls as per nginx settings

Example

    location / {
    root   /path/to/drupal;
    index  index.php index.html;
 
    try_files $uri $uri/ @rewrite;
}
 
location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
}
Geo V L
  • 866
  • 8
  • 25