Odd Mod_Rewrite Behaviour - .htaccess

I've had a look at a few mod_rewrite questions here but nothing they have suggested has worked to date.
I have the below rules:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]*)/$ ?action=$1 [L]
## General Pages
RewriteRule ^privacy-policy/$ ?action=privacy-policy [L]
RewriteRule ^terms-conditions/$ ?action=terms-conditions [L]
RewriteRule ^about/$ ?action=about [L]
RewriteRule ^contact-us/$ ?action=contact-us [L]
RewriteRule ^sitemap.xml$ ?action=sitemap [L]
## Blog Pages
RewriteRule ^blog/$ ?action=blog [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ ?action=blog&category=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ ?action=blog&category=$1&id=$2&title=$3 [L]
The first blog rule works fine and the third blog rule works fine.
The second blog rule isn't working correctly, lets say I have the below URL:
/blog/general/
The second blog rule is returning blog/general as the value for categorywhich is incorrect, it should only be returning general as the value for category.
This issue only occurs on my production environment, it doesn't happen on my local dev environment.
I'm really stumped on this one, any suggestions greatly appreciated.

Try reordering your rules and merge SSL with www rule to avoid multiple redirects (bad for SEO and client experience):
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
## General Pages
RewriteRule ^privacy-policy/$ ?action=privacy-policy [L]
RewriteRule ^terms-conditions/$ ?action=terms-conditions [L]
RewriteRule ^about/$ ?action=about [L]
RewriteRule ^contact-us/$ ?action=contact-us [L]
RewriteRule ^sitemap.xml$ ?action=sitemap [L]
## Blog Pages
RewriteRule ^blog/$ ?action=blog [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ ?action=blog&category=$1 [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ ?action=blog&category=$1&id=$2&title=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]*)/$ ?action=$1 [L,QSA]

Related

Convert display language to subfolder with htaccess

For a website I have locally, I need to set the display language as if it were a subfolder, using an htaccess file.
For example, if I choose to display the site in German, instead of displaying it like this => local_ip/test/index.php?lang=de, I would like it to be displayed like this => local_ip/test/de/ .
Also, if I type in the url without setting the language, I would like htaccess to automatically redirect to the German language, from so => local_ip/test/tour/mountain/ to so => local_ip/test/de/tour/mountain .
My current htaccess file is structured as follows:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
SOLVED
After several attempts and after reading several support requests posted in this community, I found a way to make the system recognise all the other rules as well. All I had to do was put them before the last rule provided by the user who supported me. Here is the final code.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]
You can use the following :
RewriteEngine on
#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]

Htaccess redirecting dynamic urls

I am trying to create redirections from the old urls to the new ones and I have a problem how to write appropriate htaccess rules.
old urls
domain.com/cat1/cat2/cat3/city,name
domain.com/cat1/cat2/city,name
domain.com/cat1/city,name
new urls
domain.com/cat1/cat2/cat3?city=name
domain.com/cat1/cat2?city=name
domain.com/cat1?city=name
I tried something like this but it doesn't work.
RewriteRule ^/city,(.*)$ /city=?$1 [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^,]*),(.*)/?$ $1/$2/$3?$4=$5 [L]
RewriteRule ^([^/]*)/([^/]*)/([^,]*),(.*)/?$ $1/$2?$3=$4 [L]
RewriteRule ^([^/]*)/([^,]*),(.*)/?$ $1?$2=$3 [L]
RewriteCond %{HTTP_HOST} ^www\.domain\.pl [NC]
RewriteRule ^(.*)$ http://domain.pl/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain.pl
RewriteCond %{HTTP_HOST} ^(.+).domain.pl
RewriteRule ^([^/]*)$ http://domain.pl/account/firma/%1 [P,L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
EDIT: As per OP's edit adding following now.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.pl [NC]
RewriteRule ^(.*)$ http://domain.pl/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain.pl
RewriteCond %{HTTP_HOST} ^(.+).domain.pl
RewriteRule ^([^/]*)$ http://domain.pl/account/firma/%1 [P,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^,]*),(.*)/?$ $1/$2/$3?$4=$5 [L]
RewriteRule ^([^/]*)/([^/]*)/([^,]*),(.*)/?$ $1/$2?$3=$4 [L]
RewriteRule ^([^/]*)/([^,]*),(.*)/?$ $1?$2=$3 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##To deal with domain.com/cat1/cat2/cat3/city,name URLs
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^,]*),(.*)/?$ $1/$2/$3?$4=$5 [L]
##To deal with domain.com/cat1/cat2/city,name URLs
RewriteRule ^([^/]*)/([^/]*)/([^,]*),(.*)/?$ $1/$2?$3=$4 [L]
##To deal with domain.com/cat1/city,name URLs
RewriteRule ^([^/]*)/([^,]*),(.*)/?$ $1?$2=$3 [L]
You can try this full .htaccess code. Make sure you don't have anything extra apart from this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+)\.(domain\.pl)$ [NC]
RewriteRule ^ http://%1/account/firma%{REQUEST_URI} [P,L]
RewriteCond %{HTTP_HOST} ^www\.(domain\.pl)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([^,]+),([^/]+)/?$ $1?$2=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Also make sure to clear your browser cache completely before testing this change.

Htaccess same rule for 2 files not working

I am trying to create friendly pagination using htaccess file.
But its not working I guess I am using wrong rule for files.
Check out my codes below.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/$ cat.php?id=$1 [NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]
Everything is working fine, only cat2.php is not working.
How to fix it?
You need to switch the order around between your 2 cat rules. (.*) matches everything, including slashes, so it will always match whatever cat2 matches. Try just swapping their orders and include L flags:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?q=([^\s&]+) [NC]
RewriteRule ^ /search/%1/? [R=301,L]
RewriteRule ^search/$ search/%1 [L,R=301,NC]
RewriteRule ^search/(.*)/$ search.php?q=$1 [L,NC]
RewriteRule ^new/(.*)$ new.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ cat2.php?id=$1&page=$2 [L,NC,QSA]
RewriteRule ^(.*)/$ cat.php?id=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id=$1 [NC]

.htaccess remove index.php remove www. force https

I understand this is not necessarily an EE specific question, but it involves the classic EE removal of index.php from the URL as well as a couple more specific rewrites. It’s also a lazy question, as I’ve tried a few rewrite rule combinations and there are errors… so, what I’m after is (please).
Remove www.
Force https://
Remove index.php
The important note, is that on an (mt) Media Temple server when testing on subdomains such as dev.domain.com the good old multiple redirects problem is kicking in. Ideally there’s a way for one .htaccess file to rule them all. Many thanks in advance.
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Force https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
What worked for me (excluding the index.php part, only for removing WWW and forcing HTTPS), was this:
# Enable Rewrite Engine
RewriteEngine On
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force https, specify http_host
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#-----the following part is untested, but should work to answer the full quesiton including the index.php part
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/css/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/fonts/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/themes/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/inc/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/admin.php$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/index.php?$1 [QSA,L]
</IfModule>

htaccess - redirect to ignore nested subdirectory and if possible make entire site non-www

I have an issue that's too complex for me to handle, but I'm betting someone has had to do this before, so please let me hear from you. ;)
Here's the situation:
I've got 1 main domain with 3 subdirectories that are nested within each other
(from top to bottom)
http://main-domain.com
then
http://main-domain.com/company-name/
then
http://main-domain.com/company-name/blog/
There's currently 3 .htaccess files -- 1 in each of the 3 directories shown above.
What's the problem?
Instead of having www.main-domain.com/company-name/blog/whatever, I'd like to have main-domain.com/blog/whatever
So, I want to drop the www AND more importantly, drop the middle subdirectory; i.e. /company-name/
I hope that the following examples will help to illustrate the point.
http://main-domain.com/company-name/index.php should be changed to http://main-domain.com/index.php
http://main-domain.com/company-name/blog/my-first-article/ should be changed to http://main-domain.com/blog/my-first-article/
Why do I need this?
I need a shorter URL that is more SEO-friendly. I have too many backlinks that use the 'old' urls, so I need to mod-rewrite them all.
Here are My Current 3 htaccess files
root htaccess: main-domain.com
#Bypass InoCore Templating System
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /reservations/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]
Options -Indexes
</IfModule>
#END Bypass
#301 REDIRECT
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
company-name htaccess: main-domain.com/company-name/
RewriteEngine on
RewriteRule ^maping.php /maping.php
RewriteRule ^$ index.php?$1 [L]
RewriteRule (.*) index.php?$1 [L]
#php_flag magic_quotes_gpc off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]
</IfModule>
#END WordPress
blog htaccess: main-domain.com/company-name/blog/
RewriteEngine off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]
</IfModule>
#END WordPress
Your correct and compact root .htaccess should be like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
NC is for ignore case comparison
$1 is your REQUEST_URI matching group

Resources