Forum Moderators: phranque

Message Too Old, No Replies

Apache Not Exactly Working As I Expected

         

MagickCrafter

2:44 am on Jan 23, 2007 (gmt 0)

10+ Year Member



I recently changed from static web pages to .PHP dynamic web pages, and after reading, as I suspected grr, .PHP pages are not very SEO friendly. This is mainly due to the fact that .PHP pages add the SESSID invisibly to the URL, so search engines hate the URLs. Plus, www.example.net/mysite/ looks much better than www.example.net/php/master.php?param=index.

So, I resorted to APACHE mod-rewrite for the answers. I read about how PHP can do this by "Exploding" the URLs, but I felt curious about Rewrite, mostly because a friend mentioned it for my forums.

Anyways, back on topic: I want to mass replace my entire website URLs into their .PHP equivalents.

Soooo (sort of in this logical order):

http://example.net/ ---> http://www.example.net/ (301)
--------------
http://www.example.net/ ---> http://www.example.net/index/ (301)
--------------
http://www.example.net/index ---> http://www.example.net/index/
--------------
http://www.example.net/whateverhere/ ---> http://www.example.net/content/index.php?param=whateverhere
--------------
http://www.example.net/flash/whateverhere/ ---> http://www.example.net/content/flash/index.php?param=whateverhere
--------------
http://www.example.net/portfolio ---> http://www.example.net/portfolio/ (301)
--------------
http://www.example.net/portfolio/ ---> http://www.example.net/content/portfolio/index.php?param=index

(NOTE that the ones listed as 301 I want to VISIBLY redirect)

I have tried many rewrite things in my .htaccess file, but none to exactly do what I want. For example, I have done this to rewrite everything to their local .php equivalents: (notice I don't want them to locally rewrite, I want them to rewrite to the /content folder)

DirectoryIndex index.php
RewriteEngine on
rewritecond %{http_host} ^example.net [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc]
##########
rewriterule ^(.*)/ index.php?root=$1
rewriterule ^(.*)/ index.php?root=$1

But that doesn't work like I want it.... Plus if I have a subfolder that I DON'T want to rewrite, it rewrites anywayz.... THIS IS IMPORTANT I DON'T WANT THIS TO HAPPEN! Another reason I don't like this is because of DirectoryIndex index.php. This means search engines index the .php page :( But I can't figure out how to redirect http://www.example.net/ to http://www.example.net/index/

I really need help on this one guys, thank you so much. .htaccess just is a weird language for me for some reason.... If you can offer any help please do post :)

[edited by: jdMorgan at 3:00 am (utc) on Jan. 23, 2007]
[edit reason] examplified. [/edit]

MagickCrafter

7:07 pm on Jan 23, 2007 (gmt 0)

10+ Year Member



I sort of figured it out, now if I use a subdomain (I presume) the .htaccess won't do anything because the host is not "www.example.net" correct? It would be like "subdomain.example.net" or would it still be "example.net"? Anyways here is the .htaccess:

########################################################################
##########Start rewrite engine. index.php really doesn't exist#########
########################################################################
DirectoryIndex index.php
RewriteEngine on
########################################################################
#########Rewrite All Requests of example.net To WWW Equivalents#########
########################################################################
rewritecond %{http_host} ^example.net [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc,L]

########################################################################
###If there is simply a blank directory request (e.g. www.example.net)##
##########################rewrite it to .htm############################
########################################################################
RewriteCond %{http_host} ^www.example.net [nc]
RewriteCond %{REQUEST_FILENAME} -d
rewriterule ^(.*)$ $1/index.htm [r=301,nc,L]

########################################################################
######################Rewrite .html to .php file########################
########################################################################
RewriteCond %{http_host} ^www.example.net [nc]
rewriterule ^(.*).html?$ http://www.example.net/content/index.php?root=$1 [L]
########################################################################
#################Rewrite contactform.php to index.php###################
########################################################################
RewriteCond %{http_host} ^www.example.net [nc]
rewriterule ^contactform.php$ http://www.example.net/content/index.php?root=contactform [L]
########################################################################
###################Rewrite roller.php to index.php######################
########################################################################
RewriteCond %{http_host} ^www.example.net [nc]
rewriterule ^flash/roller.php(.*) http://www.example.net/content/flash/roller.php$1 [L]

[edited by: MagickCrafter at 7:09 pm (utc) on Jan. 23, 2007]

jdMorgan

7:30 pm on Jan 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's really not clear what you're trying to do here.

Your "rewrites" are actually coded as 302-Moved Temporarily redirects, since they include the "http://www.example.com" part in the substitution URL. A check with a server headers checker, such as the "Live HTTP Headers" extension for Firefox and Mozilla browsers, will confirm this.

For example:


RewriteCond %{http_host} ^www.example.net [nc]
rewriterule ^(.*).html?$ http://www.example.net/content/index.php?root=$1 [L]

would normally be written:


RewriteCond %{http_host} ^www\.example\.net [NC]
RewriteRule ^(.*)\.html?$ /content/index.php?root=$1 [L]

This rewrites the URL www.example.com/<whatever>.html to www.example.com/content/index.php?root=<whatever>

Note that characters which have meaning as regular expressions tokens must be escaped in your patterns as shown by preceding them with a backslash. Otherwise, a period within a pattern will be interpreted as meaning "any single character" rather than as a literal period.

As to your question about subdomains and subdirectories, that all depends on how your server is configured to map subdomains to subdirectories. Either each subdomain is mapped to a separate subdirectory (usually under your 'main' domain's top-level Web-accessible directory), or alternatively, all subdomains are mapped to your top-level Web-accessible directory, and you are left to sort them out by using .htaccess.

It helps when considering these issues to note that URL-space, used on the Web, is different from file-space, used within the server. It is your server's job to 'map' your URL-space to its filespace, and as mod_rewrite makes clear, this mapping is not necessarily direct. mod_rewrite functions during the URL-to-filename translation phase. So, once you begin rewriting a URL using mod_rewrite in .htaccess, domains, hosts, and URLs become meaningless; You are dealing only with local URL-paths and/or filepaths. Making this distinction between URL-space and filespace can clarify many of the finer points of mod_rewrite's applications.

Hopefully this is more helpful than confusing...

Jim

MagickCrafter

11:25 pm on Jan 23, 2007 (gmt 0)

10+ Year Member



That makes sense... Sort of.

So, if I do this:

rewriterule ^(.*).html?$ http://www.example.net/content/index.php?root=$1 [L]

instead of this:

rewriterule ^(.*).html?$ /content/index.php?root=$1 [L]

They are entirely different? I want it to appear as for example www.example.net/mainindex.html but actually be www.example.net/content/index.php?root=mainindex

I got it to work like this:


# Start rewrite engine
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Rewrite All Requests of EXAMPLE.net To WWW Equivalents
rewritecond %{http_host} ^example.net [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc,L]
#
# If there is simply a blank directory request (e.g. www.example.net) rewrite it to .htm
RewriteCond %{http_host} ^www.example.net [nc]
RewriteCond %{REQUEST_FILENAME} -d
rewriterule ^(.*)$ $1/index.htm [r=301,nc,L]
#
# Rewrite .html to .php file
RewriteCond %{http_host} ^www.example.net [nc]
RewriteRule ^(.*)\.html?$ /content/index.php?root=$1 [L]
#
# Rewrite contactform.php to index.php
RewriteCond %{http_host} ^www.example.net [nc]
RewriteRule ^contactform\.php$ /content/index.php?root=contactform [L]
#
# Rewrite roller.php to index.php
RewriteCond %{http_host} ^www.example.net [nc]
RewriteRule ^flash/roller\.php(.*) /content/flash/roller.php$1 [L]

Thats correct right?

One more question: how would I go about converting /blahblah.htm(l) into just /blahblah/?

So:

http://www.example.net/test.html --> http://www.example.net/test/

and that goes to the index.php file. (So my question is what would the rule be instead of RewriteRule ^(.*)\.html?$ /content/index.php?root=$1 [L]? I would assume it would be RewriteRule ^(.*)/$ /content/index.php?root=$1 [L] but that doesn't work..)
[edited by: MagickCrafter at 12:02 am (utc) on Jan. 24, 2007]

[edited by: jdMorgan at 2:39 pm (utc) on Jan. 24, 2007]
[edit reason] Tidied-up for readability, examplified. [/edit]

jdMorgan

2:45 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to re-read what I wrote above about literal character escaping, and research the difference between the rewrite and redirect forms of RewriteRules before proceeding -- There are several errors remaining that may cause you trouble, and which have already been discussed above.

You may also find this thread [webmasterworld.com] to be useful.

Jim