MrVimes
MrVimes

Reputation: 3312

How do I get two htaccess rewrite rules to 'play nice' together?

In my website root I have the following to redirect to non www domain

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]

in a subfolder named 'photography' I have this...

RewriteEngine on
RewriteRule ^show/([^/\.]+)/([^/]+)$ show.php?section=$1&photoid=$2 [L]

Anything inside the photography folder ignores the www removing rule. How do I get these two rules to both apply to folders/files within the photography folder?

Also... my root htaccess file has this...

# 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

Could it be interfering? I have a self-hosted wordpress blog but it's not in the root of the website, it's in a subfolder called 'blog' so I don't know why this rule is in my root's htaccess file. Can/should I move it?

Edit: Just to point out, in case it isn't obvious - I'm a complete noob when it comes to htaccess and mod_rewrite stuff. Does a htaccess file in a subfolder override any htaccess files nearer to the root than it? Or do the htaccess contents combine?

Edit 2: I have tried moving the second rule to the same htaccess file as the www removing rule as per the following code...

# 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

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ photography/show.php?section=$1&photoid=$2 [L]

If I then go to one of my photography pages it resolves to the intended url (http://notails.com/photography/show/pointofayre/260 for example) but the page is a 404.

If I manually add 'www' to that it undoes the other rule... (http://notails.com/show.php?section=pointofayre&photoid=260) and removes 'photography/' from it.

Upvotes: 1

Views: 2579

Answers (1)

LazyOne
LazyOne

Reputation: 165501

Add RewriteOptions inherit into .htaccess in your "photography" folder because right now all rewrite rules from parent folders are ignored (default behaviour).

Alternatively move rewrite rules from that subfolder .htaccess into root one (you will need to slightly modify your rule by fixing the path -- adding photography/ may be enough, depends on actual "photography" location)


UPDATE: Your root .htaccess can be like this:

Options +FollowSymlinks

<IfModule mod_rewrite.c>
    # activate rewrite engine
    RewriteEngine On
    # we are in the root
    RewriteBase /

    # no www please
    RewriteCond %{HTTP_HOST} ^www\.notails\.com [NC]
    RewriteRule ^(.*)$ http://notails.com/$1 [R=301,L,QSA]

    # photography
    RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ /photography/show.php?section=$1&photoid=$2 [L]

    # WordPress rules
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Upvotes: 3

Related Questions