Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to convert /filename.php to /filename/

Help with rewrite rule syntax and exclude some directories

         

jdcpar5

11:27 am on Oct 15, 2007 (gmt 0)

10+ Year Member



I'm looking for help using mod rewrite to map filenames to virtual directories.

If my file is [mydomain.com...] I want it to appear as [mydomain.com...]

I also want [mydomain.com...] to work when typed as an address or clicked as a link and if someone types in [mydomain.com...] WITHOUT the trailing slash, I'd like the rewrite rule to add it.

Also need to prevent these rules from executing in the /blog/ and /cgi-bin/ directories.

I can't seem to get this from the online docs at [httpd.apache.org...]

Any advice would REALLY be appreciated.

jdMorgan

1:58 pm on Oct 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort code as a basis for discussion.

Also, consider the /foo/ is seen as a directory, while /foo is seen as a file. If your extensionless page URLs have an appended slash, you'll likely have more difficulty keeping search engine results clean. There's also the issue that browsers will resolve relative links on page URLs having a trailing slash to a subdirectory with that page name, so all of your on-page links will need to be server-relative or absolute -- or alternatively, you'll need to rewrite all those linked URLs. In short, best advice is to omit the slash if /foo is a page.

Jim

jdcpar5

4:04 pm on Oct 15, 2007 (gmt 0)

10+ Year Member



So far, I have:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
RewriteRule ^/.php([^/]+)/?(.*) /$1/ [R]

While foo.php is a file on my server saved as www.mysite.com/directory/foo.php, I just want my URLs to show www.mysite.com/directory/foo/

I read about mapping a file to a virtual directory, but (as you can tell) I'm in over my head.

My thinking is that if we change from php, to say asp or whatever, we can still always have our URLs as /directory/foo/ instead of having to redirect foo.php to foo.asp.

g1smd

7:44 pm on Oct 15, 2007 (gmt 0)

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



Be aware that you can have one or more RewriteCond lines that test the requested URL, but they will apply to only the single RewriteRule that immediately follows them.

I always add a blank line after each RewriteRule to remind that the next line is treated as being a separate rule. It may need its own prefixed RewriteCond lines.

Be aware of the problems caused when a redirect feeds another redirect. You need to avoid creating a redirection chain of any sort.