Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule and redirect not working in htaccess

         

ipco

8:59 pm on Apr 21, 2016 (gmt 0)

10+ Year Member



I recently moved from a flat site to a cms. The pages remained the same except for removing the .html extension
when I try to access the site using the .html it goes to my 404 page instead of to the new page.

I have tried both Rewrite and redirect without luck.

this is my .htaccess file. What am I doing wrong?

Thanks.

#
# GetSimple CMS htaccess ROOT file
# apache 2.4
#

# The following require certain allow overrides, if getting 500 error comment them out one by one
# can be resolved in apache httpd.conf to ensure security alternatives

# override charset
AddDefaultCharset UTF-8

# prevent directory listings
Options -Indexes

# Follow symbolink links, This is required for rewrites on some hosts
Options +FollowSymLinks

# Set the default handler.
DirectoryIndex index.php

# blocks direct access to the XML files - they hold all the data!
<Files ~ "\.xml$">
<IfModule !mod_authz_core.c>
Deny from all
</IfModule>
<IfModule mod_access_compat.c>
Deny from all
</IfModule>
<IfModule mod_authz_core.c>
<IfModule !mod_access_compat.c>
Require all denied
</IfModule>
</IfModule>
</Files>

<Files sitemap.xml>
<IfModule !mod_authz_core.c>
Allow from all
</IfModule>
<IfModule mod_access_compat.c>
Allow from all
</IfModule>
<IfModule mod_authz_core.c>
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
</IfModule>
</Files>

# handle rewrites for fancy urls
<IfModule mod_rewrite.c>
RewriteEngine on

# Usually RewriteBase is just '/', but
# replace it with your subdirectory path
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
</IfModule>

RewriteEngine on
RewriteRule ^firstPage\.html http://www.example.com/firstPage [R=301,L]
RewriteRule ^secondPage\.html http://www.example.com/secondPage [R=301,L]

# redirect 301 /firstPage.html http://www.example.com/firstPage
# redirect 301 /secondPage.html http://www.example.com/secondPage

[edited by: engine at 8:54 am (utc) on Apr 22, 2016]
[edit reason] please use example.com [/edit]

ipco

12:09 pm on Apr 22, 2016 (gmt 0)

10+ Year Member



After I wrote this, I realized my mistake. Just a matter of having things in the correct order. Everything is now good.

lucy24

8:07 pm on Apr 22, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



having things in the correct order. Everything is now good.

Well, not really, because you've still got mod_alias (Redirect by that name) and mod_rewrite in the same htaccess, and you shouldn't. Ordering of directives only makes a difference within any one module; it doesn't affect the execution order of modules. Ordinarily mod_rewrite executes before mod_alias. (As a rough rule of thumb: things go in reverse alphabetical order, unless you've got a weird server.) So you need to change any mod_alias rules to mod_rewrite, and then put them before any CMS rules.

Edit: You should also get rid of all those <IfModule> envelopes. Not their contents, just the envelopes. They're the hallmark of boilerplate htaccess rules that could be plugged in anywhere. Once you're on a specific site living on a specific server, you either have the mod or you don't. Write the rules accordingly.

ipco

11:54 pm on Apr 22, 2016 (gmt 0)

10+ Year Member



Thanks Lucy for your advice on this.
when you mentioned mod_alias, where you talking about the redirects? They were commented out but are now removed. I was just trying one over the other as neither was working.

Apart from my RewriteRules, everything else was preloaded, so yes they are boilerplate. htaccess is unknown territory for me and I'm treading very carefully but I'm ready to take the journey. I just need some pointers.

<IfModule> envelopes: are you suggesting this

<Files ~ "\.xml$">
<IfModule !mod_authz_core.c>
Deny from all
</IfModule>
<IfModule mod_access_compat.c>
Deny from all
</IfModule>
<IfModule mod_authz_core.c>
<IfModule !mod_access_compat.c>
Require all denied
</IfModule>
</IfModule>
</Files>

should become this

<Files ~ "\.xml$">
Deny from all
Require all denied
</Files>

or am I way off track?

lucy24

6:21 am on Apr 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



when you mentioned mod_alias, where you talking about the redirects? They were commented out but are now removed.

Yes: Any redirect using the word "Redirect" (or "RedirectMatch") is a mod_alias directive. You can achieve the identical result with mod_rewrite. For example

Redirect 301 /firstPage.html http://www.example.com/firstPage
is the same as
RewriteRule ^firstPage\.html http://www.example.com/firstPage [R=301,L]

Or if you started out using RedirectMatch, when you're going extensionless:
Redirect 301 /(firstPage|secondpage|thirdpage)\.html http://www.example.com/$1
=
RewriteRule ^(firstPage|secondpage|thirdpage)\.html http://www.example.com/$1 [R=301,L]
meaning "dump the .html but keep the rest of the URL".

should become this

Yikes, no, so you need to find out what Apache version you're on. If it's 2.2 or earlier (or even 2.4 with mod_compat-thingummy) then it's
Deny from all
The exact name of the module doing the deed varies from one version to another, but the syntax of the directive is always the same so you don't really need to know it. But the
Require all denied
syntax is new in 2.4 and can't be used earlier.

In practice, the main purpose of an <IfModule> envelope is to prevent a 500-class error if you use a directive that the server can't understand. (Fun fact: The stuff inside an <IfModule> envelope doesn't actually need to have anything to do with the capabilities of the module. You're just telling the server to either read the lines or ignore them.)