Page 1 of 1

htaccess redirect not existing file (404) as 302 to another

Posted: Wed Mar 19, 2014 8:32 pm
by lovelf
Doing pagination on a site through lazy load:

I would want to redirect

dir/546/index.html
if it does not exist

to

/dir/index.html
546/ is of any numeric value.

Re: htaccess redirect not existing file (404) as 302 to anot

Posted: Wed Mar 19, 2014 9:34 pm
by Celauran
Perhaps something like this?

Code: Select all

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/dir/([0-9]+)/?(index\.html)?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /dir/index.html [R=302,L,NC]

Re: htaccess redirect not existing file (404) as 302 to anot

Posted: Wed Mar 19, 2014 11:11 pm
by lovelf
Yes, almost there, how might use /dir/ as wildcard for any directory name.

Thanks

Re: htaccess redirect not existing file (404) as 302 to anot

Posted: Thu Mar 20, 2014 8:46 am
by Celauran
Perhaps with another capture group? Replace ^/dir with the appropriate regex and use a lookback rather than a hardcoded value for the rewrite.