Forum Moderators: phranque

Message Too Old, No Replies

URL without slash not working

rewrite still showing slash at URL

         

ccal68

7:29 pm on Dec 1, 2008 (gmt 0)

10+ Year Member



Hello everyone, I need help and hope you all can help me to solve my problem on htaccess.

I would like my URL look like this: (at browser bar)
http://www.example.com/folder/abc

and the actual webpage at here:
http://www.example.com/folder/abc/index.htm

However, when running htaccess my URL address keep on redirect from "/abc" to "/abc/index.htm".

Below is my htaccess code:
---------------------------------------------------------------
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} folder
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !/$
Rewriterule ^(([^/]+/)*[^./]+)$ /$1/index.htm [L]

----------------------------------------------------------------

Can anyone help me to find out error on my htaccess code. Thanks.

g1smd

10:31 pm on Dec 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it a one-step redirect or a chain of multiple steps?

Use Live HTTP Headers for Firefox to get that information.

Does it perhaps go via example.com without www included?

jdMorgan

11:12 pm on Dec 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That code looks over-complicated and has several redundancies. Although I doubt that these problems are the cause of your trouble, you might want to try:

Options +FollowSymLinks
RewriteEngine on
#
# If requested URL does not resolve to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# internally rewrite '/folder/<path>' URLs with no slash or period
# in the final path-part to /folder/<path>/index.htm
RewriteRule ^(folder/[^./]+)$ /$1/index.htm [L]

If there is any mod_rewrite code that does external redirects in your .htaccess file, this internal rewrite rule must be placed *after* those redirects. If there is any mod_alias code that does redirects in your .htaccess file, then I would recommend re-writing that code using mod_rewrite directives, and again, placing this new rule after those redirects. This is needed so that you can strictly control the execution order of the directives, which is not possible if the directives are processed by different Apache modules.

Jim

ccal68

7:41 pm on Dec 2, 2008 (gmt 0)

10+ Year Member



g1smd, It is go with www.example.com (included www), the Live HTTP header showing 301 Moved Permanently.

ccal68

7:43 pm on Dec 2, 2008 (gmt 0)

10+ Year Member



jdMorgan, thank you for your code, I have tested it by putting it in my .htaccess file, there are no other mod_rewrite code existed in this file except the code that you suggested. the end result are as follow:

When typed in browser bar:
http://www.example.com/folder/abc

it will 301 redirected to and showing in browser bar as follow:
http://www.example.com/folder/abc/

(with slash included at the end of URL)

Any idea that might cause the trouble?

g1smd

12:21 am on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's looks like the standard redirect to add the trailing slash to URLs that are for folders, as per the HTTP specs.

ccal68

5:08 am on Dec 3, 2008 (gmt 0)

10+ Year Member



Is there anyway to overcome this problem if using .htaccess file?

g1smd

8:30 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, there are ways round it, but you are well advised to not break the HTTP specifications unless you are quite happy for a search engine to suddenly decide they can't access your site properly and drop all your pages from their index. Happy to proceed, or do you want to rethink your URL format?

jdMorgan

1:18 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you hosted on Apache 2.0 or later?

If so, then you may need to specify:


DirectorySlash off

in your .htaccess file.

See Apache 2.x mod_dir documentation for more info.

Jim