Need help setting up a redirect map

I cannot get my rewrite map to work. I have read a lot of pages on this and yet can’t figure out how. I have a feeling my rewritecond may be wrong. So some tips would be helpful. :slight_smile:

I have the following in my httpd.conf and took an apache restart

RewriteMap ErrorMap txt:/usr/local/apache/conf/userdata/std/1/techencl/site.com/vb2xf.map

The rewrite map has the following

corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/

The .htaccess in the root directory has the following entry

RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]

Edit: Just posting my current .htaccess file contents for reference

<IfModule mod_rewrite.c>
    RewriteEngine On

RewriteRule [^/]+/.+-([\\d]+)/p([\\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\\d]+)/ showthread.php?t=$1 [NC,L]


Options +FollowSymlinks
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
</IfModule>

SS,

For others, you MUST have access to the httpd.conf (or httpd-vhosts.conf) to be able to use a RewriteMap. SS does have this so …

Let me comment on each of your code bits:

RewriteMap ErrorMap txt:/usr/local/apache/conf/userdata/std/1/techencl/site.com/vb2xf.map

That seems to be okay. However, if you’re having problems, mod_rewrite may need a .txt extension (but I doubt it).

corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/

That’s the correct format for the map.

The .htaccess in the root directory has the following entry

RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]

This is where I believe you’re confusing mod_rewrite. IMHO, you don’t need the RewriteCond. Remember, the second $1 in your redirection is supposed to be the not_found script. These errors are likely to be your problem.

<IfModule mod_rewrite.c>

[indent][color=RED][standard rant #4][indent]The definition of insanity is to repeatedly do the same thing expecting a different result.  Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world.  DON'T ACT INSANE!  If you don't know whether a module is enabled, run the test ONCE (without the wrapper) then delete it permanently as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server).[/indent][/standard rant #4][/color]

For what it's worth, my opinion is that ANY abuse of the server is unacceptable, moreso when it's repeated multiple times for every request being made of the server (which should be in the thousands if not hundreds of thousands).[/indent]

    RewriteEngine On

RewriteRule [^/]+/.+-([\\d]+)/p([\\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\\d]+)/ showthread.php?t=$1 [NC,L]

[indent]Linux is case sensitive (for path/file, not domains) so the No Case flag can cause you to match unacceptable content. You're not specifying in your regex other than "p" and digits so I have to wonder why NC was included.[/indent]


Options +FollowSymlinks
rewritecond %{http_host} ^domain[size=6]\\[/size].com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]

[indent]Same comment as above.[/indent]

</IfModule>

[indent]Same comment as above.[/indent]


Regards,

DK

Thanks for the detailed reply dklynn!

Sorry I could not reply any sooner as my laptop crashed and I could not make any changes to the config. I implemented most of your advice except for NC for now. I will look into it after I am done with this setup.

The rewrite rule turns a link - http://www.site.com/corner/lock-unlock-140859.html
into
http://www.site.com/-------/home/encl/public_html/home/encl/public_html/---------/corner/lock-unlock-140859.html

With - /home/encl/public_html/ repeating around 20 or so times. Which is the site root path on the server.

SS,

Loopy code is generally caused by the inappropriate use of (.*).

[standard rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/standard rant #1]

If you would like more (better) comments, please display your mod_rewrite code and the test (and destination) URIs.

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK

Will go through that tutorial in your sig.

Meanwhile this is the content from the live site
ErrorMap:

reviews-and-previews/philips-she9800-review-130025.html http://www.techenclave.com/community/threads/philips-she9800-review.62537/
audio-zone/buy-speakers-for-using-at-home-201436.html http://www.techenclave.com/community/threads/buy-speakers-for-using-at-home.122640/
audio-zone/sound-card-confusion-179140.html http://www.techenclave.com/community/threads/sound-card-confusion.102379/

.htaccess

#	Mod_security can interfere with uploading of content such as attachments. If you
#	cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#	SecFilterEngine Off
#	SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default


RewriteEngine On


RewriteRule [^/]+/.+-([\\d]+)/p([\\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\\d]+)/ showthread.php?t=$1 [NC,L]

rewriterule ^(.*) ${ErrorMap:$1|$1} [R=301]

	#	If you are having problems with the rewrite rules, remove the "#" from the
	#	line that begins "RewriteBase" below. You will also have to change the path
	#	of the rewrite to reflect the path to your XenForo installation.
	#RewriteBase /xenforo

	#	This line may be needed to enable WebDAV editing with PHP as a CGI.
	#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Options +FollowSymlinks
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

From httpd.conf

RewriteMap ErrorMap txt:/home/te/public_html/vb2xf.txt

</VirtualHost>

Tried modifying the rewrite rule based on the tutorial. Still no luck. :frowning:

rewriterule ^([a-z0-9A-Z\\-]+\\.html)$ ${ErrorMap:$1|NOTFOUND} [R=301]

SS,

Whoops! Sorry I missed that one as it’s a clear indication that your Apache has an error and needs to be restarted.

Is NOTFOUND a script (to handle “I couldn’t find that ‘garbage’”)?

From Apache.org:

indent]For example, you might define a RewriteMap as:

RewriteMap examplemap txt:/path/to/file/map.txt

You would then be able to use this map in a RewriteRule as follows:

RewriteRule ^ex/(.*) ${examplemap:$1}

A default value can be specified in the event that nothing is found in the map:

RewriteRule ^ex/(.*) ${examplemap:$1|/not_found.html}[/indent]

Please note that their use “the default value” was to provide an absolute (internal) link to a script.

I also removed the /s after ^ because that had broken mod_rewrite code upon the change to Apache 2 - it had been required by Apache 1.x.

Regards,

DK

Got it to work!

Ok I ignored some of your advice on garbage atom but lets take one step at a time. Thanks a lot for the help and your tutorial! :smiley:

rewriterule ^(.*\\.(html)) ${ErrorMap:$1|/not_found.html} [R=301]

SS,

Good! Take some time and go through a (.*) redirection as if you were Apache and see what happens.

Regards,

DK