Apache: Problems with redirects

2019-08-02 23:05发布

问题:

I have the next rules, and I should be able to access to this URLs without any problem:

domain.com/robots.txt - should redirect me to -> domain.com/sitemapHT/robots.txt now robots.txt has an index of xmls files, where does the sitemaps for different sections of the site write its sitemap. For example: domain.com/sitemapHT/sitemap_anuciantes.xml

With the current rules (below) When I try to access to any of the /robots.txt or sitemapHT urls, it sends me to domain.com/

The rules also are redirecting from old /portal/index.action url we have, to a new just /

Could you help me to fix the rules to access properly to the previous urls?

  <VirtualHost *:80>
        ServerAdmin administracion.linux@yellargentina.com
        DocumentRoot "/opt/tomcat-5.5.30/webapps/portal"
        ServerName hoteles.t1.yellargentina.com
        ServerAlias hoteles.t1.yellargentina.com
        CustomLog /tmp/hoteles-access_log combined
        ErrorLog /tmp/hoteles-error_log
        RewriteLog /tmp/hoteles-rewrite_log
        RewriteLogLevel 3

        JkUnMount       /images/*.gif           w1
        JkUnMount       /images/*.png           w1
        JkUnMount       /images/*.jpg           w1
        JkUnMount       /js/*.js                w1
        JkUnMount       /styles/*.css           w1


        JkMount         /portal                 w1
        JkMount         /portal/*               w1


        RewriteEngine on

        RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
        RewriteRule .* - [F]
        Options +FollowSymlinks


        ##
        # Originales
        ##

        ##
        RewriteRule ^/robots.txt /sitemapHT/robots.txt [R=301]
        RewriteCond $1 !^/(sitemapHT/|portal/)
        ##

        #RewriteRule ^/$ /portal/ [R]
        #RewriteRule ^/(?!portal)(.*) /portal/ficha.action/$1 [PT]

        RewriteRule ^/$ /portal/index.action [PT]
        RewriteRule ^/portal/index.action / [R]
        RewriteRule ^/(?!portal)(.*) /portal/ficha.action/$1 [PT]
</VirtualHost>

Thank you!


回答1:

These 2 lines together make no much sense:

RewriteCond $1 !^/(sitemapHT/|portal/)
RewriteRule ^/robots.txt /sitemapHT/robots.txt [R=301]

This single line will do the job of rewriting (if you want redirect add R=301, next to L) domain.com/robots.txt into domain.com/sitemapHT/robots.txt:

RewriteRule ^robots\.txt$ /sitemapHT/robots.txt [L]

P.S. You rewrite log (the last code block in question body) is unreadable. Please format it properly.

P.P.S. If you have other rules in your .htaccess, they may affect this rewrite rule. It is better if you provide whole .htaccess if this one will not work.


UPDATE:

1. No need for this line: RewriteCond $1 !^/(sitemapHT/|portal/), especially if it after rewrite rule.

2. What these 2 lines do?

RewriteRule ^/$ /portal/index.action [PT]
RewriteRule ^/portal/index.action / [R]

As for me -- they do opposite jobs. But if it works then I will not touch it.

Try these rewrite rules:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F,L]

RewriteRule ^/robots.txt /sitemapHT/robots.txt [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\s/portal/index.action\sHTTP/.+
RewriteRule ^/portal/index.action$ / [R,L]
RewriteRule ^/$ /portal/index.action [PT,L]

RewriteRule ^/(?!portal|sitemapHT)(.*) /portal/ficha.action/$1 [PT,L]