Forum Moderators: phranque

Message Too Old, No Replies

Can anyone help with my .htaccess ?

         

lostoldaccount

10:54 am on Jul 13, 2009 (gmt 0)

10+ Year Member



Hi all, I'm not new here, just couldn't seem to get my old account to work. Anyway I would appreciate a little help if you would be so kind.

I have a web address as this :
[mysite.co.uk...]

My .htaccess file not allows me to view above as this :
[mysite.co.uk...]

However ideally I would like to virtually remove the 'sites' from the url, so :
[mysite.co.uk...]

TBH ideally i would like also to create a virtual subdomain :
[london.mysite.co.uk...] something similar.

I would really appreciate any help on this matter, even if it is just to remove the sites folder.

My .htaccess file is as follows:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*) viewsites.php?name=$1&page=$2 [L]

I have searched and searched for a solution, but everything i try seems to give me a 'page cannot be found error'

many thx
jon

jdMorgan

3:44 pm on Jul 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need to make a decision about one particular issue regarding your plan: If you remove the "/sites" path from your URLs, the server will need some alternative 'clue' as to whether a URL being requested by a client is a 'site' or not. If you remove that path info, then the only thing the server can do is to pass all extensionless URL requests to your script, and let the script decide whether it can handle that URL as a 'site.' If not, the script will need to open the file corresponding to that URL, read it in, and send it back to the client along with all appropriate HTTP response headers.

You'll likely also want to support the "If-Modified-Since" requests from clients. To make a long story short, removing this bit of path information will mean that your script will have to handle many more requests, and will become much more complicated as a result. And if you have other extensionless URLs that are NOT 'sites,' then there really is no solution short of redesigning your entire URL architecture and URL layout. The '/sites' path information is something that appears to be needed to make your site work properly.

(You could change '/sites' to some other 'word' if you like, as long as that word is fairly unique.)

BTW, a more robust and *much* more efficient rule for your existing setup would be:

 RewriteRule ^([^/]+)/([^/]+)$ viewsites.php?name=$1&page=$2 [L]

You should avoid using multiple ".*" subpatterns in your rules, as they are extremely inefficient to process; They can require dozens, hundreds, or even thousands of 'back-off-and-retry' matching attempts in order to determine a match that satisfies the entire pattern. The solution I present here can be evaluated in a single left-to-right pass and on a busy and growing site, can forestall the need to upgrade your server for years.

Jim

lostoldaccount

4:37 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



Thank you, thank you Jim.

I think i get the 'jist' of what you are saying, and appreciate your reworking of my .htaccess file. I will put that into practice.

I wish i had thought about the urls before i started coding the site, my other big problem is knowing very little about .htaccesss rewrite rules.

My obvious solution would be to move the viewsites.php file out of the 'sites' folder and into the root. This would eliminate my problem, BUT (along with other problems i will face including the php basedir) I don't know how to get the .htaccess to work. I can't seem to just move it into the root folder where i would have thought it would work.

Oh well, i'll keep trying. But appreciate if you have any further thoughts

many thx
jon.

jdMorgan

6:36 pm on Jul 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For use in the root .htaccess, the code would have to change to:

RewriteRule ^sites/([^/]+)/([^/]+)$ sites/viewsites.php?name=$1&page=$2 [L]

URL-paths in .htaccess files are 'localized' and the path to that .htaccess file's directory is stripped. So, moving your code 'up' one level means that the pattern will now have to recognize the 'sites/' prefix, because it will now be present in the URL-path 'seen' by RewriteRule.

Jim

lostoldaccount

6:53 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



Thx again Jim.

Before I try that I moved viewsites.php into the root, then copied the original .htaccess out of the sites folder and into the root also.

The problems were: the style sheet refused to work and the images, even thought the links to both are not relative.

Secondly (and most important) it messed up all my other files, not just in root but also in other folders. For instance the root index.php was stripped of any css and was trying to send vars to the database. (as viewsites does).

I will try your above suggestion now though.

Sorry for not being exactly clear, it is difficult to explain properly when you are a novice and don't know the terminology

cheers
jon

lostoldaccount

7:08 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



I was thinking maybe i should include a link to try show what i mean.
This is a database driven simple site:
http://www.example.co.uk/sites/specimen-site/homepage

As you know i'm would ideally like to remove the sites folder from the address bar.

viewsites.php is in the sites folder. When I move this file into the root the .htaccess file messes up all the other pages within the whole domain.

Please let me know if more info is required.

many thx
jon

[edited by: jdMorgan at 8:15 pm (utc) on July 13, 2009]
[edit reason] Sorry, no links to personal sites. [/edit]

jdMorgan

8:52 pm on Jul 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at the last code I posted, and note that the pattern starts with "sites/".

If you don't do that, then your original rule, moved to example.co.uk/.htaccess will rewrite *any* requested URL-path of the form
/<anything or nothing>/<anything or nothing>
to viewsites.php?name=$1&page=$2

Your external CSS files, images, external JavaScript files, and other included objects will not work unless you refer to them as <img src="http://www.example.co.uk/<path-to-image-directory>/image-name.gif">
or as <img src="/path-to-image-directory>/image-name.gif">
and exclude those paths from being rewritten to your script as well. (Note that this is what 'sites/' is doing for you now.)

That is, you must use a canonical URL or a server-root-relative URL-path (starting at "/" -- the home directory or your site).

Realize that it is the client (e.g. the browser) that resolves relative URLs to canonical URLs. It does so based on the 'page address' URL showing in its address bar. So if you are rewriting URLs to a different directory level, the browser will be basing its requests on the directory level in its address bar, and will be requesting images from that directory -- unless you give it a canonical or server-relative (and not a page-relative) include path.

If you've got all these included-object paths right and still have a problem, it may be due to browser caching -- Be sure to completely flush (delete) your browser cache before testing any new server-side code.

Again, you can change 'sites/' to something else in the address bar, or make it "sites-" instead of "sites/", but if you delete it completely you may well regret it. Realise that it is only "sites/" that is telling your server (via mod_rewrite) to pass the request to the "viewsites" script. If you remove that information from the URL, then viewsites.php will be the only script you can ever have... only... one... ever... forever... unless you change your domain or are willing to give up all of your hard-earned links and search rankings.

This is a big bad bear waiting to bite you, and I suggest you reconsider 'cosmetics' versus the severely-limited future of your site. If you want to replace "sites" with a shorter string or a "better keyword," then go for it. But be very very careful about how completely-eliminating it will affect your options for further developing your site in the future.

Now, if you'd be satisfied to limit yourself to one and only one script on this site forever, or some very-inefficient solutions that may well force an early upgrade to a dedicated server, we can discuss other options, but I want you to mentally 'sign off on that' before assisting in what I consider to be a very bad idea.

One option might be:


RewriteRule ^([^/]+)/([^/.]+)$ viewsites.php?name=$1&page=$2 [L]

which takes any requested URL-path that appears to start with a subdirectory (name) followed by an extensionless 'filename' (page), and rewrites it to /viewsites.php with the name and page in the query string.

The only restriction with that is that your 'page' names may not ever contain a period or a slash.

Jim

lostoldaccount

10:05 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



I really appreciate your help Jim, good of you to explain everything so cleary and of course take the time.

I take everything you have said onboard and will take your advice to leave it 'as is', or re-code the site to remove the sites folder.

It is a relatively new site and is just a freebie I am creating for submariner association branches, just so they can have a presence on the www and hopefully generate further membership.

I was looking for a simple quick way of making the addresses easier to remember and friendly, but tbh it isn't that bad.

many many thx again

kindest regards
jon