Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other How to remove subdirectories using mod_rewrite in .htaccess Reply To: How to remove subdirectories using mod_rewrite in .htaccess

#170982
__
Participant

example.com/keyword/ and example.com/keyword will be considered as 2 different pages in the eyes of Google…

You can solve this by including a canonical meta tag on the page (this is good practice anyway).

I still prefer to standardize my url to all having a trailing slash.

Note that your rule would not do so. Rewriting a URL does not change what appears in the address bar unless you specifically tell the browser to re-request the URL (i.e., by specifying an external domain, or by using the R flag).

To illustrate, assume the user types “example.com/some/url” in their address bar.

  • case #1:
RewriteRule ^(.*)$ /myPage.html [L]

# apache rewrites the url to "example.com/myPage.html".
# the user sees "myPage.html",
# but address bar *still reads* "example.com/some/url".
  • case #2:
RewriteRule ^(.*)$ http://other.example.com/theirPage.html [L]

# apache rewrites the url to "other.example.com/theirPage.html".
# because this is on a different host, apache sends a Redirect header
# (by default, "302 Found") to the user's browser.
# the user's browser will request the rewritten url,
# and the address bar will then read "other.example.com/theirPage.html".
  • case #3:
RewriteRule ^(.*)$ /myPage.html [L,R=302]

# apache rewrites the url exactly as in case #1,
# but because you used the [R] flag will also send a Redirect header as in case #2.
# the user's browser will request the rewritten url,
# and the address bar will then read "example.com/myPage.html".

To be clear, I’m not suggesting you force the user to redirect. There’s no need. Rewrite internally, include a canonical reference in your html, and save everyone the trouble of turning one request into two.

I went over to look for the difference between REQUEST_URI and REQUEST_FILENAME, http://httpd.apache.org/ doesn’t explain well.

Do you understand the difference between a URI (or URL, which is a type of URI) and a filename?

A URI is like a name/ nickname for some resource (which might be a file, or might not). It defines what you want.

A filename is the actual name of a file on your computer. It is like an address on your hard disk — it defines where something is.

Many times, on websites, a URI will map directly to a particular file in a very predictable 1:1 manner. But it does not have to, and they are certainly not the same thing.

Even in this simple usage, the two will be different: for example, the URI directory/file.html does not map to the filename /directory/file.html. It maps relative to your site’s document root, so the actual filename will be something like `/users/ewisely/webserver/public_html/directory/file.html’.