简体   繁体   中英

RewriteRule Apache Directives in .htaccess not working

My .htaccess file for my PHP wordpress app is in clone-directory/php/ and I could say that it was recognized by the OpenShift server. But some of the RewriteRules and RewriteCond doesn't seem to work. Since the app is accessible by both http and https by crawlers, I'm trying to create separate robots.txt for ssl and http connections.

I've been researching hours and hours with several different codes but none of them work. Some of them are as follows ...

1

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^robots.txt$ robots-https.txt [L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

2

<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks 
RewriteCond %{SERVER_PORT} ^443$ 
RewriteRule ^robots.txt$ robots-https.txt [L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

3

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^robots.txt$ robots-https.txt [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

4

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} ^on$ [NC]
RewriteRule ^robots.txt$ robots-https.txt [L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

5

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^robots.txt$ robots-https.txt [L]

When I visit https://reversiblean-reversiblean.rhcloud.com/robots.txt , the url won't redirect. URL is always the same.

Keep it like this:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^robots\.txt$ /robots-https.txt [L,R,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Make sure the robots.txt and the second robots.txt exist in the repo's php directory. Unlesss OpenShift will point it to a default robots.txt, though I'm not sure where that file is stored. I was expecting for the url to change, but only the contents of the robots.txt would change according to the http protocol.

I can't believe I've spent almost half of my day on this. : )

In case anyone else was having trouble or wondering about Google's duplicate content issue, here's how my .htaccess looks now ...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^robots.txt$ robots-ssl.txt [L]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM