Forum Moderators: phranque

Message Too Old, No Replies

Updating .htaccess: taking off the .html and shortening the url

htaccess shortening the url

         

heavensbest

9:18 pm on Mar 4, 2011 (gmt 0)

10+ Year Member



Okay, so I have this working on my old version of my CMS, modx 1.0.4. But I want to get it working for the new version, modx 2.0.7, which I can't get it working.


This is what I had:
RewriteRule ^([^/]+)_([^/]+) /folder/$1_$2.html [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule Pattern Substitution [OptionalFlags]

RewriteRule ^(.*)\.html index.php?q=$1 [L,QSA]


I'm not sure why it won't work on the new CMS, but it may have something to do with this line of code breaking the new CMS.
RewriteRule ^(.*)\.html index.php?q=$1 [L,QSA]


So they replaced it with:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]



This is what I want to do:
- rewrite any request that doesn't have a period '.' after www.domain.com/ to /folder/file.html
- not be required to have to have the underscore '_' (the reason I was required to do that is because it was the only way I could get it working the way I wanted to in order to shorten the url)


I've tried lots of things, but can't figure it out. I'm using MODX 2.0.7.

g1smd

2:01 am on Mar 5, 2011 (gmt 0)

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



# Rewrite all .html URLs to the script.
# $1 contains only the filename, not the extension.
RewriteRule ^(.*)\.html index.php?q=$1 [L,QSA]


# Rewrite all URLs to the script.
# $1 will contain the both the filename and the extension.
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


I would have expected something like this:
RewriteRule ^([^/.]+)\.([^/.]+)$ index.php?q=$1&ext=$2 [L,QSA]

but only if the PHP script also detected $ext as well as $q.

I don't understand why ModX designers don't properly utilise the power of mod_rewrite to:
- go extensionless, and
- improve the rewrite efficiency and therefore the whole site speed.

In particular the -f and -d "exists" checks are the absolute worst way of coding this stuff.

heavensbest

4:02 am on Mar 5, 2011 (gmt 0)

10+ Year Member



So, I understand what you are saying with your first two examples. Is the third example the answer to my question?

The new modx you can go extensionless, it is just that the site I am transferring is not, except the pages that I am trying to make work.

Does your third example allow for documents in different directories to have the same name?
ie.
/this/samefilename.html
/that/samefilename.html

All I want to do is have a file in example.com/folder/file.html, but access it by example.com/file. Every other file will have either a back-slash '/' or a period '.' . That way I can fill my /folder full of files and not make my root gigantic with files, not to mention messy.

heavensbest

7:46 am on Mar 5, 2011 (gmt 0)

10+ Year Member



Is there a way to see what the .htaccess file is doing?

Like to see what is happening when I try these lines in it? I tried each line separately. None of them worked. But can you at least see what I'm trying to do?
RewriteRule ^([^\.]+)$ /folder/$1.html [NC]
RewriteRule ^([^.]+)$ /folder/$1.html [NC]
RewriteRule ^([^/]+) /folder/$1.html [NC]

heavensbest

7:52 am on Mar 5, 2011 (gmt 0)

10+ Year Member



Should it matter if the old site is at domain.com and the new site, for now, is at subdomain.domain.com?

jdMorgan

2:25 am on Mar 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rewrite logging is available only if you have server config access -- which is unfortunately not the case if you're one of the 99% of Webmasters using shared hosting.

However, you can be sure that if the code is in .htaccess in the correct directory-path, then it will rewrite as you have coded it to do. You need to be very careful to avoid recursion though: If the rewritten path matches the pattern of your rule, then that path will be rewritten again and again and again... So some of your "test rules" won't work because they will recurse.

Since the rules above are all internal rewrites and do not refer to a protocol or hostname, they should work on either domain.

Are you saying you want requests for extensionless URLs to be served from static .html files in the /folder subdirectory if such a file physically exists, and otherwise, you want the requests passed to your CMS script?

Listing out several example URLs and the required disposition under all possible conditions is really the first step -- you have to define the requirements completely and precisely before attempting to code...

Jim