homepage Welcome to WebmasterWorld Guest from 54.167.174.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess in WordPress?
What does this code do in blog.domain.com?
web2008

5+ Year Member



 
Msg#: 4627174 posted 1:46 pm on Dec 2, 2013 (gmt 0)

What does this code do in blog.domain.com
.htaccess for WordPress do?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4627174 posted 4:59 pm on Dec 2, 2013 (gmt 0)

Everything. It's what makes WordPress (or any similarly configured CMS) work.

Do you need a detailed answer?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4627174 posted 10:04 pm on Dec 2, 2013 (gmt 0)

:: looking around irritably for someone else to answer ::

OK, long version :)

What does this code do in blog.domain.com
.htaccess for WordPress do?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


<IfModule mod_rewrite.c>
...
</IfModule>

means: This is a boilerplate htaccess file constructed so as not to crash the server, even if it's installed by some nitwit who actually doesn't have the use of mod_rewrite (and who would, therefore, not be able to use this CMS). The foregoing is, ahem, not the technical meaning of the "IfModule..." envelope. But it's what it functionally means. Get rid of both lines.

RewriteEngine On
Required for each separate instance of mod_rewrite, because it uses different inheritance rules from everything else in Apache. That means: once in the config file, once in each htaccess that uses mod_rewrite, once more if you're using mod_rewrite in a <Files> section within mod_rewrite ... et cetera.

In boilerplate the line should be accompanied by
Options +FollowSymLinks
In practice it isn't needed, because if the server is set up to allow mod_rewrite within htaccess at all, this option has already been set. (Because otherwise any shared host would be flooded with tech-support requests saying "Why don't my RewriteRules work?!")

RewriteBase /
means: if the target of a RewriteRule begins in a path alone (no protocol, host or even directory slash), append / to the beginning. In practice it's better to put an explicit / in each target, so the line isn't needed-- especially since / is the default.

RewriteRule ^index\.php$ - [L]
means: if the server receives a request for the file "index.php" in the directory that contains the RewriteRule, stop here and don't look at the rest of mod_rewrite in the present htaccess file or <Directory> section.

RewriteRules should be followed by a blank line. Not for any Apache reason, just to make them easier to read. Each ruleset-- RewriteRule plus any preceding Conditions plus any # comments pertaining to the rule-- should similarly be preceded by a blank line.

RewriteRule . /index.php [L]
means: no matter what the request really says, serve content from example.com/index.php --and then stop looking for any more RewriteRules.

This rule is the heart of WordPress and many other popular CMS. It means that, no matter what the browser's address bar says or what page the robot thinks it's on, WordPress will quietly build a page and send it back to the user. The non-CMS, hand-rolled equivalent might look something like
RewriteRule ^(dirname)/(filename)\.html /some-other-page.php?dir=$1&page=$2 [L]
Your CMS will do this part for you after the request lands on the index.php page.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

In mod_rewrite, conditions go before the rule they belong to. Conditions are only evaluated if the rule itself potentially applies. In the present case, that means: if the request has content. This specifically excludes requests for the current directory-- or your domain name alone, if the rule is in the root-- making the previous rule irrelevant.

The first condition means: exclude requests for named files that physically exist. The second, similarly, means exclude requests for named directories that physically exist. This pair of conditions is meant to exclude two things: non-page files such as images or stylesheets, and any hard-coded pages (including directory-index pages) you might happen to have.

:: insert further boilerplate about how wasteful and inefficient the -f and -d system is ::

Edit:
The htaccess file can't be located in blog.example.com, because servers don't operate in three dimensions. There are separate processes to, first, tell the browser where to go when it has been told to fetch subdomain.example.com, and then, second, tell the server "If you get a request for subdomain.example.com, go to such-and-such directory". Most often it's a directory that's physically inside* the directory for example.com, but it doesn't have to be. It doesn't even have to be on the same server.


* For, uhm, a given definition of "physically".

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved