I am sure I am doing something silly here, but I can't seem to figure out what it is.
I can't understand why the RewriteCond for the {REQUEST_URI} isn't evaluating to true and thus preventing the RewriteRule from firing. Instead, given the following url, the rule fires and the user is always taken to https://www.diffsite.com.
Any help would really be appreciated - and thanks in advance.
url:
http://www.regularsite.com/page-name/
.htaccess code:
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod|android|symbian|windows\phone|blackberry|iemobile|opera\ mobile|palmos|webos|googlebot-mobile) [NC]
RewriteCond %{REQUEST_URI} !^page-name/ [NC]
RewriteRule ^(.*)$ https://www.diffsite.com/ [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
%{REQUEST_URI} always matches a URI that starts with leading forward slash / so this will work for you:
RewriteCond %{REQUEST_URI} !^/page-name/ [NC]
However in your rule RewriteCond %{REQUEST_URI} is not even needed as you can do this negation in RewriteRule itself:
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod|android|symbian|windows\phone|blackberry|iemobile|opera\ mobile|palmos|webos|googlebot-mobile) [NC]
RewriteRule !^page-name/ https://www.diffsite.com/ [NC,L,R=302]
Not that inside .htaccess leading slash is not matched in RewriteRule.
Related
I'm using mod-rewrite to make my URLs prettier. Everything is working finde except for index.php.
Every php-file gets parameter for the language, e.g. contact.php?lang=de. The file can be called by /contact/de. Since en is the default language, it should not occur in the URL: /contact. This is working fine. The same should apply for ìndex.php?lang=... with the difference, that the English (default) version is / (instead of /index) and the German version is /index/de (the same as for contact).
Options +FollowSymLinks
RewriteEngine on
# No trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+)/$ htt p://%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule contact$ contact.php?lang=en [L] # default language
RewriteRule contact/(.*)$ /contact.php?lang=$1 [L] # any other language
# default language (don't show any thing in the URL)
RewriteRule index/en$ / [L,R=301]
# requesting /index.php, one should be forwarded to /
RewriteRule index.php$ / [L,R=301]
RewriteRule /$ index.php?lang=en [L] # default language
RewriteRule index/(.*)$ /index.php?lang=$1 [L] # any other language
http://domain/index/en is working properly, i.e. the URL is forwarded to http://domain/. http://domain/index/de is forwarded to http://domain/?lang=de. This is correct but the URL should not be changed. Even if I remove any RewriteRule with R=301 in my htaccess-file, the URL is still changing. I have to clue why this happens. I'm doing the same that works correctly for /contact. Are there any implicit rules for index.php?
You can use:
Options +FollowSymLinks
RewriteEngine on
# No trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteBase /
RewriteRule contact$ contact.php?lang=en [L,QSA] # default language
RewriteRule contact/([^/]+)/?$ contact.php?lang=$1 [L,QSA] # any other language
# default language (don't show any thing in the URL)
RewriteRule index/en$ / [L,R=301]
# requesting /index.php, one should be forwarded to /
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteRule ^/?$ index.php?lang=en [L,QSA] # default language
RewriteRule ^index/([^/]+)/?$ /index.php?lang=$1 [L,QSA] # any other language
i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.
I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.
Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$
I have been trying to get my urls re-written. The first 4 rules are vital, but they are clashing with this line: (i think).
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
this stops the url being able to be accessed like so www.example.com/page.php and redirects to www.example.com/page/
after adding the first 4 rules you can see in the htaccess, the above condition doesnt seem to work and Im able to access urls like this :(
lovelakedistrict.com/lake-district-cottages.php/cottages/5/
obviously i should be like this
lovelakedistrict.com/lake-district-cottages/cottages/5/
however this still works :)
lovelakedistrict.com/lake-district-cottages.php -->
lovelakedistrict.com/lake-district-cottages/
This is my htaccess file- can anyone see what happening?
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
#these 4 rules stop being able to access pages like eg
#/lake-district-cottages/?cottages=5/
#/lake-district-cottages/?cottages/5/ --> all direct to /lake-district-cottages/cottages/5/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
# this stops you accessing pages with php extention eg
#/lake-district-cottages.php --> directs to /lake-district-cottages/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# this ignors the include folder and does not add trailing slash (so ajax file works)
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
#this removes php extention and adds trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#these re-write urls allowing / to replace ? and =
RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
RewriteRule ^lake-district-hotels/hotels/([0-9]+) lake-district-hotels.php?hotels=$1
RewriteRule ^lake-district-bed-and-breakfast/bed-and-breakfast/([0-9]+) lake-district-bed-and-breakfast.php?bed-and-breakfast=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^lake-district-cottages-shop/lake-district-book-shop/([0-9]+) lake-district-cottages-shop.php?lake-district-book-shop=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^result/([a-zA-Z0-9])/([0-9]+) result.php?$1=$2
RewriteRule ^shop-result/([a-zA-Z0-9])/([0-9]+) shop-result.php?$1=$2
The condition pattern doesn't match your URLs that contain PATH_INFO, but simplifying it a bit to something like this should take care of that:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301,L]