Forum Moderators: phranque

Message Too Old, No Replies

.htaccess question that might be easy for all of you

         

skuba

1:11 am on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi,
I am not very familiar with .htaccess, but I believe this is probably very simple for you.
That's what I want to do:
Setup a .hatacces that will add an extension .shtml to all links on my sites. Or at least to all links on files in a certain folder (this would be better)
Let's say that I my links are like <a href="/files/file1>
So, .htaccess would be make that every access to that file actually calls for /file/file1.shmtl.

It is possible? and Can I make some exceptions for the rule. Like jus tdo it for files in a certain folder?

PS: The site is all static with some SSI. No CGI.

I really appreciate your help.
Thanks a lot,
Yan

skuba

1:14 am on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



Even better, would be a rule that check if the file has no extension than add .shtml...

possible?

That would save me a lot of time!

Thanks

jdMorgan

3:53 am on Nov 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



skuba,

This Introduction to mod_rewrite [webmasterworld.com] thread may serve as a good introduction to rewriting URLs. While we are happy to help you debug your code, we generally prefer not to write your code for you, as stated by our charter [webmasterworld.com].

In order to keep the threads posted here valuable to more than just one person or a very few people, we prefer to discuss Apache configuration, .htaccess, and mod_rewrite, rather than to provide a free code-writing forum. We would much prefer to help you learn to write the code yourself if that's what you want. If not, posting your specific requirements in the Commercial Exchange [webmasterworld.com] may help you to find a contractor to write the code for you for a reasonable fee.

Take a look at the mod_rewrite thread cited above, and please post any questions you may have.

Jim

skuba

10:19 am on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



I've been doing some research and trying to code it myself. But all examples I find are for rewriting like renaming the file extension. Like changing from a .txt to a .html.
In my case, I don't want to rename an extension but add one.
The links on the site would point to file without extensions, but I want to use some code that will add a .shtml to the links.
So, you see, it won't necessarily rename the file or add an extension to the file, I want to add an extension to the link.
The file can even be let say file1.shtml, but on the link it will call for file file1 only.
Or, if you prefer, I can have all files without extension, but that way I need the code to add the extension .shtml to the links and files and also make those files parse like .shtml files.
Do u undertand?
Thanks for helping

skuba

10:36 am on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



I am trying something like this
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!\.shtml$
RewriteRule $1\.shtml ^/(.*)

but won't work

closed

4:54 pm on Nov 21, 2003 (gmt 0)

10+ Year Member



Try changing your RewriteRule to this:

RewriteRule ^(.*) /$1\.shtml [L]

I reversed the order of the pattern and substitution, removed the slash because you're using .htaccess, added a slash to the beginning of the substitution, and added the L flag to stop rewriting after executing the rule.

skuba

5:25 pm on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month




Thanks for trying to help. But I get a 500 error.

Let me make something clear that might help. We could do this in 2 ways.
1 - The links points to a file without extension href=file1 , but we can do that the file itself has the extension.shtml . So what we need to do is just add the extension to the link, not to the file

2 - Both file and links have no extension
so href=file1 will open file1 but will be dysplayed as a .shtml .
Maybe all we need to do is actually change the parser.

Thanks

skuba

7:52 pm on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



Lets make it easier.

{On my site I have a URL} poker.shtml. The links point to file called "card-game".
The way it is now, it will actually open file card-game, but it's considering that it's just a txt file, while it actually is a SHTML, there are some SSI in there.

So, I want to try a way to make every call to file card-game in a way that the server will parse it as a shtml file.

OR

The other idea is, I rename card-file to card-file.shtml. Then what I need to do is make a htaccess that will add extension .shtml to the link "card game" pointing to card-game (with no extension).

See what I mean?
Thanks all for helping.

[edited by: jdMorgan at 6:08 am (utc) on Nov. 22, 2003]
[edit reason] No specific URLs, please [/edit]

skuba

9:31 pm on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



I guess issue is solved with
<FilesMatch ^[^./]+$>
SetHandler server-parsed
</FilesMatch>

skuba

11:37 pm on Nov 21, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



Ok. This worked. But I found out that DreamWeaver doens't save files with no extension. And my site is based on templates created at dreamweaver.

So, if anybody still have an idea of how to make links with no extension point to a file.shtml, I will be very thankful.
Thanks a lot.

Yan

jdMorgan

6:24 am on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



skuba,

I would recommend solution number 2 from your post, msg#8 above. Rename the actual files with a .shtml extension. This will eliminate the need for the AddHandler directive.

Then, you will need to rewrite all pathnames *that are not* (sub)directories or all pathnames *that are* files to add the .shtml extension. (There may be other reasons you don't want to do this, but rewriting this way will be inefficient, so you might want to consider using a multi-file search-and-replace editor to fix all the links on your site quickly and easily.)

The key is to use the -f flag of RewriteCond to check the filepath to see if it is a file or a directory. If the requested resource is a file, and its URI contains no period (therefore no file extension), then add .shtml. This will add overhead to every file you serve, though.


# If local URL contains no periods
RewriteCond %{REQUEST_URI} ^([^\.]*)$
# If requested filename is a file, not a directory
RewriteCond %{REQUEST_FILENAME} -f
# Add .shtml extension
RewriteRule ^(.*)$ /$1.shtml [L]

Jim

skuba

7:30 am on Nov 22, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



Jim, I got it done with the code:
Options +FollowSymlinks
RewriteEngine on
# if the URI doesn't contain a dot and doesn't contain a slash
# then make an internal redirection to the file with the .shtml
RewriteRule ^([^./]+)/?$ $1.shtml [L]

Could you let me know what code I have to add if I also want it to place an underscore "_" where there are spaces on the file names?

so that file called this if file 1 , be renamed to this_is_file_1

but, it needs to be a rewrite. Like on the browser it would also show the underscores?

Is it possible?
Thanks again.

jdMorgan

8:02 am on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That pattern does not do what you want. "." is a special character meaning "match any single character" in mod_rewrite regular expressions, and so must be escaped by preceding it with "\" if you wish to match a literal dot.

# if the URI doesn't contain a dot and doesn't contain a slash
^([[b]^\./[/b]]+)/?$ /$1.shtml [L]

For info on how to substitute characters in URLs, please see this recent thread: [webmasterworld.com...]

Jim

skuba

8:32 am on Nov 22, 2003 (gmt 0)

10+ Year Member Top Contributors Of The Month



The code worlks for me, because my file are without extension. So, I don't want to match the dot.
But, any file wihout a dot or slash. It's working.

Now, to change spaces to underscore. I saw the other topic.
But, is there a code for space, or I can just use a space like that:

RewriteRule ^([^_]*)_([^ ]*)$ http://www.example.com/$1_$2 [R=301,L]

?

[edited by: jdMorgan at 2:34 am (utc) on Nov. 24, 2003]
[edit reason] De-linked URL [/edit]

jdMorgan

7:58 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



skuba,

I'm afraid you DO want to match a dot. You want to match it, and then take action only if there is *no* dot present. I am not telling you that you have a logic problem, I am telling you that you have a syntax error.

The regular expression "[^.]" will match only blank URLs because it says "NOT any single character". Because any non-blank URL will contain at least a single character, only a blank URL will match that pattern.

Similarly, the pattern "[^./]" will match only a blank URL because the match still fails if *any* character is present, and therefore, adding the slash does nothing.

To match URLs that do not contain a dot or a slash, you need "[^\./]" or "[^/\.]".

If you want to match a dot or a space or any character in the list below, either positively (character is present) or negatively (character is not present), you must precede it with a backslash "\"

.+*?^$%(){}[]\¦ or <space>

To match a space in a URL (example /blah blah.html), use ^blah\ blah\.html$

Ref: [etext.lib.virginia.edu...]

Jim