Tobias
Tobias

Reputation: 319

htaccess, www redirect fake subdirectory

Common problem, but too complicated for me.

Here are the requirements I am trying to meet:

  1. The root URL http://example.com should be redirected to http://www.example.com
  2. All URLs like http://example.com/c/1234567890 should be redirected to http://www.example.com/c/1234567890 (notice the "c" fake subdirectory)
  3. When entered http://example.com/index.php one should be redirected to http://www.example.com (no trailing slashes)
  4. On top of that I'm trying but failing to secure a subfolder "xy" from direct access exept from php files in root and javascript files.

I searched a lot and tried a lot of rewrite conditions and rules, but htaccess+regex is just from another planet for me. :/ Sorry if this is a duplicate…

Upvotes: 1

Views: 485

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

These would be in the htaccess file in your document root. Turn on the rewrite engine

RewriteEngine On

For 1 and 2: redirect to "www" if root request or request for /c/(something)

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(c/.+)?$ http://www.example.com%{REQUEST_URI} [L,R=301]

For 3: redirect direct requests for /index.php to just /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^ / [L,R=301]

For 4: forbid direct access to anything in the /xy/ directory.

RewriteCond %{HTTP_REFERER} !http://(www\.)?example\.com/ [NC]
RewriteRule ^xy/ - [L,F]

Upvotes: 2

Related Questions