Forum Moderators: phranque
I want to re-write my websites url in this way:
http://www.example.com/temp-members/anypage.php
TO
[example.com...]
How can I achieve this?
Similarly, Can I do it with multiple folders?
<snip>
Saad
[edited by: jdMorgan at 3:50 pm (utc) on Feb. 1, 2010]
[edit reason] Removed personal appeal. [/edit]
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]
RewriteEngine On
#
# Externally redirect to remove .php from URLs;
# Test THE_REQUEST to prevent infinite redirect/rewrite
# looping due to interaction with internal rewrite below
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP
RewriteRule ^(.+)\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect to remove "index" or "index/"
RewriteRule ^([^/]+/)*index/?$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect to remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite extensionless URLs to add .php to filepath, excluding
# requests with a trailing slash or a "filetype" in the final URL-path-part
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
RewriteRule ^temp-members/(.*)$ /members/$1 [L]
If you use this rule, it would also be a good idea to change all the links on your site from "/members/<anything>" to "/temp-members/<anything> and to externally redirect direct client requests for "/members/<anything>" to "/temp-members/<anything> in order to avoid duplicate-content problems. To be clear, for any given 'page' on your site, one and only one URL should be allowed to directly-access it; All other URL variations should 301-redirect to that one unique URL for the page in question.
Implementing this latter function requires testing THE_REQUEST, in a manner similar to the "remove .php" rule at the top of your code.
If on the other hand, you wanted an external redirect from URL-path /temp-members/<anything> to URL www.example.com/members/<anything>, that would be:
RewriteRule ^temp-members/(.*)$ http://www.example.com/members/$1 [L]
Depending on which function you really want, the rule would have to go in different places in your existing file. Further, the internal rewrite option would really need to be integrated with your "add .php" rewrite, so that both .php and non-.php requests to "/temp-members" would get both rewritten to the correct directory-path and have the .php added if needed by a single rule.
For more information, please see our Apache Forum Charter, the documents cited therein, and our Apache Forum Library.
Jim
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(members/.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://%{HTTP_HOST}/members/$1 [R=301,L]
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !^members/
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
[edited by: jdMorgan at 2:25 am (utc) on Feb 6, 2010]
[edit reason] example.com [/edit]