.htaccess rewrite engine help

ryanmm

Member
Messages
38
Reaction score
0
Points
6
my site is laid out like so:

public_html/index.php
public_html/everythingelse/(all other files here)

Im trying to figure out how to remove everything in the browser's url bar after the .com in my site. so,

http://mysite.com/everythingelse/blah.php

would be rewritten as:

http://mysite.com

and http://mysite.com/index.php

would also be rewritten as:

http://mysite.com

I also have a fixed ip and ssl, and would like to redirect all traffic to everything except mysite.com/index.php to https. so, everything inside of my everythingelse folder is not accessible via regular http, only via secure https

anybody know the htaccess rewrite engine well enough to be able to do this?
 

MaestroFX1

Community Advocate
Community Support
Messages
1,577
Reaction score
60
Points
0
just remember this : filesystem takes the first precedence

and "everythingelse" is a directory as you have mentioned above.
 

vv.bbcc19

Community Advocate
Community Support
Messages
1,524
Reaction score
92
Points
48
In Cpanle, you can directly put a redirect so that it writes to .htaccess.or
I can give you how I do a 301 redirect.That 301 thing should be replaced by your required file.

Redirect example.com/index.php to example.com/

Another snippet that I've heard is a good idea to make sure search engines don't give you a duplicate content penalty, this will also redirect example.com/folder/index.php to example.com/folder/.

Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

If you need further help,read the following.



Method 1 - .htaccess 301 Redirect

The smoothest way to redirect your visitors is to use an .htaccess redirect. This has no delay since before a page is served to the browser the server checks first for an .htaccess file... if it sees this the old page never loads, instead visitors are sent directly to the new page.

These are a few .htaccess redirect codes that I've used that might come in handy for you. This is not a complete list by any means, but it took me ages to find how to do these so I'll save you the hassle and list them here. Oh, and please don't email me with questions about how these work, like I said, I found these with the help of others.. I have no idea in the slightest how to write this stuff and take no credit (or responsibility) for how they work.
mportant notes about htaccess redirection

Always be sure to upload .htaccess files in ascii mode, sending it up as binary will break it (and usually make your server very, very unhappy.)
.htaccess does not work if you're on a windows server.
Make sure you triple check your changes. Clear your cache and look, test the server headers to make sure you see a 301 (that means its permanent) not a 302 (temporary) unless you are absolutely sure you really mean temporary.
Since some operating systems don't allow you to make a file without something before the "." you may need to save it as something.htaccess, some may even have to save it as htaccess.txt and change it once you've uploaded it.
Make sure your ftp program will show .htaccess files (FileZilla does and is free) It is a bit hard to edit something you can't see ;)
Double check that you're not overwriting an old one (some servers already place one there for your custom 404 pages etc.)
Make sure you replace example.com with your own sites URL ;-)

Examples

Redirect 301 /oldpage.html http://www.example.com/newpage.html

or
Redirect 301 / http://www.example.com/

Hope this helps
Regards,
VVBB
 

ryanmm

Member
Messages
38
Reaction score
0
Points
6
I copied and pasted this into my .htaccess file in public_html.

Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

Didnt seem to have any effect.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The current virtual host setup requires you to set RewriteBase. In your document root, it should be "RewriteBase /".

Your question doesn't make much sense. You say you want "http://mysite.com/everythingelse/blah.php" and "http://mysite.com/index.php" to be rewritten to "http://mysite.com", meaning anyone who tried to go to either of the first two would wind up at the third. Do you want this rewrite to be external (i.e. result in a redirect)? Since requesting "http://mysite.com" would result in a directory index being returned, you'd have to be very careful not to have a rewrite loop.
 

ryanmm

Member
Messages
38
Reaction score
0
Points
6
There are a couple things im trying to do with the rewrite.
lets forget the https ssl and just focus on the url shortening

I want every page in my site to be shortened to just "mysite.com"
it is my understanding that shortening urls doesn't actually redirect the user anyway, and to the server, the long url
like "mysite.com/user/comp.php?ID=3678764&time=209894" will both still exist and be the page that is actually served to the user,
the only thing that happens is that apache changes the way the url appears in the clients browser.
So the server sees and understands and works with "mysite.com/user/comp.php?ID=3678764&time=209894"
and the user sees whatever is on the page "mysite.com/user/comp.php?ID=3678764&time=209894"
the only thing that has changed is the url in the browsers url bar or text field or whatever you call it,
will read "http://mysite.com"
so no, its not a redirect, unless i am confused, which has been known to happen, lol

as for RewriteBase, I'm not sure what you mean by document root.
would this work?:

RewriteBase /
Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
That is not how rewrites work.

Url on the page example.com/foodle/boodle

click on it, and that is the URL that will stay in the address bar.

Rewrite can take /foodle/boodle and look internally for /boodle/foodle.php and return that, but the URL in the address bar will not change.

The only way you can make the address bar change is issue a redirect. But if you redirect to example.com , you loose /foodle/boodle and you just end up on the front page. If you redirect to example/boodle/foodle.php , that is what will end up in the address bar.

If you really want the address bar to read example.com for all your pages, use frames. The content of the display frame will change while the address bar will stay at the address of the parent frame.
 

ryanmm

Member
Messages
38
Reaction score
0
Points
6
hmmmm, so this guy just has no idea what he's talking about:

"not-so-simple rewriting ... flat links and more
You may have noticed, the above examples use regular expression to match variables. What that simply means is.. match the part inside (.+) and use it to construct "$1" in the new URL. In other words, (.+) = $1 you could have multiple (.+) parts and for each, mod_rewrite automatically creates a matching $1, $2, $3, etc, in your target (aka. 'substitution') URL. This facility enables us to do all sorts of tricks, and the most common of those, is the creation of "flat links"..

Even a cute short link like http://mysite/grab?file=my.zip is too ugly for some people, and nothing less than a true old-school solid domain/path/flat/link will do. Fortunately, mod_rewrite makes it easy to convert URLs with query strings and multiple variables into exactly this, something like..

a more complex rewrite rule:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

would allow you to present this link as..

http://mysite/files/games/hoopy.zip

and in the background have that transparently translated, server-side, to..

http://mysite/download.php?section=games&file=hoopy

which some script could process."


from:
http://corz.org/serv/tricks/htaccess2.php
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
I don't see anything in that which relates to what you want.
 

ryanmm

Member
Messages
38
Reaction score
0
Points
6
sorry, I mean no offense. Im just trying to figure this out. I like the idea of an iframe, but i have about 60 php pages and a lot of other things going on. It seems like it would take a lot more work to adjust all that to work through an iframe. If i could write a couple lines in an .htaccess file, that would be great!

what about this:
One common use of mod_rewrite is to shorten URL's. Shorter URL's are easier to remember and, of course, easier to type. An example..

beware the regular expression:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^grab /public/files/download/download.php

this rule would transform this user's URL..

http://mysite/grab?file=my.zip

server-side, into..

http://mysite/public/files/download/download.php?file=my.zip



is grab a function or method? can I use another word there?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Give a concrete example of exactly you want done. Don't leave anything out.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I want every page in my site to be shortened to just "mysite.com"
This can't work because a URI identifies a single resource at any point in time (though that resource may have multiple representations, and multiple URIs may identify that resource). The underlying mechanism that defines this is called REST (see also "How I Explained REST to My Wife"). Not only can "http://mysite.com/" not simultaneously identify (for example) "/everythingelse/blah.php" and "/index.php", you wouldn't want it to, as users could no longer bookmark individual pages. Do not try to use rewriting for this, do not use frames or iframes. Your goal should be readable URIs.

The W3C has other tips, in addition to the one on readable URIs:

In addition to being readable, HTTP URIs should be hierarchical. Keep this in mind when designing your URIs.

One common use of mod_rewrite is to shorten URL's. Shorter URL's are easier to remember and, of course, easier to type.
"http://mysite.com/" is a shortened URL only for "http://mysite.com/index.php", not for anything else. Every resource should have a separate URL, though each may be simplified in various ways.

it is my understanding that shortening urls doesn't actually redirect the user anyway,
Redirects can be internal or external. External redirects send a 3XX response to the user agent, which updates the URL in the address bar and sends a new request for the resource at the new URL. Internal redirects alter what file the URL resolves to, but do not send this information back to the user agent.

the only thing that happens is that apache changes the way the url appears in the clients browser.
The only way for a web server (such as Apache) to change the URL that appears in a browser is to send a redirect. Rewrites let you separate external and internal URLs, so that the URLs used in links don't have to have the same structure as the page components on disk. With internal redirects, it's not Apache that changes what URL is visible, the change is in the pages themselves, specifically the links.

as for RewriteBase, I'm not sure what you mean by document root.
It means in .htaccess in your document root, RewriteBase should be set to "/". In "/everythingelse/.htaccess", it would be "/everythingelse". Read about the RewriteBase directive and technical details. While you're at it, you might as well read all the rewrite docs.
 

ryanmm

Member
Messages
38
Reaction score
0
Points
6
Perhaps it would be best to explain that my site is really more of an application than a website per se. I'm not trying to do this for looks, but rather to hide my directory layout from the user. I dont want them to know the location or the URI of what page they are looking at and I dont want them to be able to bookmark it.


RULE
RewriteRule ^grab /public/files/download/download.php

EFFECT
http://mysite/public/files/download/download.php?file=my.zip
=
http://mysite/grab?file=my.zip

so it seems like i could use grab to shorten the url to whatever I want.

for example:

RULE
RewriteRule ^grab /public/files/download/download.php

EFFECT
http://mysite/public/files/download/download.php
=
http://mysite/grab

yes?

Of course I would have to make a rewrite rule for every folder and file on my site, but it's doable, yes?
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Perhaps it would be best to explain that my site is really more of an application than a website per se. I'm not trying to do this for looks, but rather to hide my directory layout from the user. I dont want them to know the location or the URI of what page they are looking at and I dont want them to be able to bookmark it.
REST principles should still underly the app. As for hiding the page URI, why? Security through obscurity?

Code:
RewriteRule ^grab /public/files/download/download.php
That should work. Note that anything that begins with "grab" (e.g. "grabble?file=my.zip") will be rewritten to the download script, which may or may not be a problem. For downloads, I'd go with "file" in the path rather than "grab"; URLs should be nouns, not verbs.

Code:
# A filename must be provided; a request for just "/file" or "/file/" will result in a 404
RewriteRule ^/?file/([^/]+) /public/files/download/download.php?file=$1 [QSA]

Of course I would have to make a rewrite rule for every folder and file on my site, but it's doable, yes?
You might be able to cover more than one script or folder with a single rewrite rule in some cases.

Code:
# Rewrites (e.g.) "/animals/parrot" to "DOC_ROOT/public/controllers/parrot.php"
RewriteCond %{DOCUMENT_ROOT}/public/controllers/$1.php -f
RewriteRule ^/?(.+) /public/controllers/$1.php

In addition to rewrite rules, you can also use rewrite maps.
 
Top