Jump to content

mod_rewrite question


MSUK1

Recommended Posts

hello,

 

i have read a lot of articles online about mod_rewrite and have decided to use it for a new project of mine.

Good news is, it works & doesnt work the code i have written wonder if anyone could just double check it for me?

 

If i have a file.. www.example.com/member.php

I would like to have the mod_rewrite function remove the .php

 

but it only seems to remove the .php from 2nd level files (domain.com/folder/file.php -> domain.com/folder/file/)

 

so i am using the following:

 

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]

 

all help appreciated,

thank you

Link to comment
Share on other sites

it does this because in the pattern it is looking for a forward slash, which will only occur in pages inside sub-folders.

 

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

Link to comment
Share on other sites

Thank you for your quick reply

 

i also found this on the web which could be of good use to extend this idea

 

RewriteEngine On
RewriteBase /

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]


# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

Link to comment
Share on other sites

it does this because in the pattern it is looking for a forward slash, which will only occur in pages inside sub-folders.

 

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

 

I just quickly tried this method, and got a 500 internal server error  :wtf:

 

-- Taking this further, how can i use the second method i posted whilst keeping the server set to "no indexing"

 

when i enabled this in cpanel i again got another 500 internal server error for this:

 

RewriteEngine On
RewriteBase /

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]


# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L][color=red][b]Options -Indexes[/b][/color]

Link to comment
Share on other sites

Well, I know that the code I posted wont trigger a 500 error by itself, because I have used this exact code on a live server.

Something else is causing the error.

Just add the "no indexing" code to the .htaccess file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.