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
https://forums.phpfreaks.com/topic/258867-mod_rewrite-question/
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

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]

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.