Forum Moderators: phranque

Message Too Old, No Replies

Rewriting Sub-Folder name using .htaccess

         

jigzyy

2:06 pm on Feb 1, 2010 (gmt 0)

10+ Year Member



Hi,

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]

jigzyy

2:08 pm on Feb 1, 2010 (gmt 0)

10+ Year Member



my current htaccess is something like 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]

jdMorgan

3:50 pm on Feb 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's clean up and optimize what's there first:

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]

You asked for an internal rewrite from URL-path /temp-members/<anything> to filepath /members/<anything>. That would be:

RewriteRule ^temp-members/(.*)$ /members/$1 [L]

This assumes that the URLs (links on your pages) are "/temp-members" but that the real filepath is "/members."

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]

The distinction between URLs and URL-paths versus files and filepaths is critical. URLs are used "out on the Web" and filepaths are used "inside this server," and they are not at all the same thing. The primary function of an HTTP server is to translate Web URLs to server filepaths; Thus the relationship between URLs and filepaths is "associative" and that "association" is determined by your server configuration (including mod_rewrite code).

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

jigzyy

10:55 pm on Feb 2, 2010 (gmt 0)

10+ Year Member



hi,
Thanks for the answer. That worked for me.

Now, If I want to enforce SSL to members directory only, how can I?

I googled and found this, but couldn't able to use.


RewriteCond %{HTTPS} off
RewriteRule (.*) [%{HTTP_HOST}%{REQUEST_URI}...]


and Should I place it in the root .htaccess or a seperate .htaccess in the directory?

Thanks

jdMorgan

3:03 pm on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The choice of where to put the code depends on your preferences for efficiency versus ease of site administration. It is more efficient to put directory-path-specific rules into .htaccess files in the directory-paths to which they apply. However, it is easier to administrate a single (or only a few) "centralized" .htaccess file(s).

In example.com/.htaccess :

RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(members/.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

or, in example.com/members/.htaccess :

RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://%{HTTP_HOST}/members/$1 [R=301,L]


You may also need to "enforce" the converse situation - "If SSL and NOT /members/, redirect to non-SSL." For example, in example.com/.htaccess :

RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !^members/
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Before installing any of this code, be sure that all links on your own site are correct with respect to "http" versus "https". This code by itself cannot fix all SSL/non-SSL linking problems, and because it issues a redirect, it slows down the visitor experience and results in 'double entries' in your logs and stats.

Jim

spikes

11:14 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



Hi, I am new to the forum and found this post similar to what I'm trying to do but cannot get it to work. I hope it's ok for me to post here.

The folder name is speedmodulefast and I want it to display as fast to the users.


http://example.com/index.php?do=/speedmodulesfast/images/


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*) index.php?do=/$1


http://example.com/speedmodulesfast/images/

Up to here it's working fine and I get the short urls instead of the long ones. Now I add the second rewrite to


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*) index.php?do=/$1

RewriteRule ^speedmodulesfast/(.*)$ /fast/$1 [L]


To achieve http://example.com/fast/images/ but its not working it still displays

http://example.com/speedmodulesfast/images/

I aprreciate your help

[edited by: jdMorgan at 2:25 am (utc) on Feb 6, 2010]
[edit reason] example.com [/edit]

jdMorgan

2:32 am on Feb 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to change the displayed link, you must edit the links on your page(s) -- or if they're produced by a script, edit the script, or buy a script plug-in that can change the on-page links.

Once that is done, reverse the 'order' of the paths in your second rule, and move it above the first rule's rewriteconds -- it won't ever run if you put it after the now-first rule. Your new first rule will then "connect" requests for your new "fast" URLs to the files in their 'real' directory at /speedmodules/fast/.

BTW, it is recommended to start your own thread, rather than walking into the middle of someone else's thread.

Jim