.htaccess rewrites a little sluggish - apache

I have the following .htaccess code that tells the server to point certain sub domains to to a sub folder and then also handles the rewriting of the root domains.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 ./404.php
RewriteCond %{HTTP_HOST} ^secretplace\.testwebsite\.local
RewriteRule ^(.*)$ /secretplace/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^payment\.testwebsite\.local
RewriteRule ^(.*)$ /payment/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^login\.testwebsite\.local
RewriteRule ^(.*)$ /login/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^signup\.testwebsite\.local
RewriteRule ^(.*)$ /signup/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^fbauth\.testwebsite\.local
RewriteRule ^(.*)$ /fbauth/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^masterapi\.testwebsite\.local
RewriteRule ^(.*)$ /masterapi/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.testwebsite\.local$ [NC]
RewriteRule ^(.*)$ http://testwebsite.local/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^testwebsite\.local$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ api.php?function=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ $1-$2.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
</IfModule>
It works, but on my local apache install the page loading is real sluggish. It takes a few seconds just to switch between pages.
Just so you know the very last line of code allows me to remove the .php extension from pages. So instead of http://testwebsite.local/features.php, it loads http://testwebsite.local/features/.
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
Is there something obvious in my code that I'm doing wrong? I'm not quite sure how to debug .htaccess.

Why are you using P flag instead of L flag ? This requires mod_proxy and can slow down htaccess execution. Seems to me you don't need it in your case.
If i had to write your code myself, it would look like this
ErrorDocument 404 /404.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(secretplace|payment|login|signup|fbauth|masterapi)\. [NC]
RewriteRule ^(.*)$ /%1/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ /index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ /api.php?function=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /$1-$2.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteRule ^.*$ /%1.php [L,QSA]

Related

/index.php/, /index.php?/, /?/ in codeigniter urls

Although we've redirected index.php to root, still we are getting inner page URLs in following 4 versions:
https://example.com/articles [/index.php/articles, /index.php?/articles & /?/articles]
Would anyone explain why?
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{http_host} ^www.example.com
RewriteRule ^(.*) example.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|phpinfo\.php|Charts|assets|uploads|userfiles|c‌​ustomer-uploads|appl‌​ication|sitemap\.xml‌​|sitemap\.html|robot‌​s\.txt|gyan\.rss)
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Try using this simpler method
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php| (.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

.htaccess issue with rerouting to https

I have this in my .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
RewriteCond %{HTTP_HOST} ^www\.clipboards\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
These condition are working fine with one exception.
If I come from http (non-ssl) it will add index.php?route to my url before sending it to https. This is not the case if I come directly from https in which case it works fine. Can someone help me with this. thanks
You should put your redirects at the top and you can combine the bottom 2 rules. Also note that you have invalid syntax [R,L=301], it should be [R=301,L]
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

htaccess redirection issue in local server

When I am trying to run my project in local server (WAMP), it is redirecting to www.localhost.com
When I type localhost/MYPROJECTNAME then it is redirecting to www.localhost.com
Please find below code:
RewriteEngine on
RewriteBase /MYPROJECTNAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Try this, it shouldn't redirect if it's localhost.
RewriteEngine on
RewriteBase /MYPROJECTNAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Negate for localhost and more importantly change the order of your rules:
RewriteEngine on
RewriteBase /MYPROJECTNAME/
RewriteCond %{HTTP_HOST} !^(www\.|localhost) [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

.htaccess redirect for complex url

I have an website with the following htaccess file:
RewriteRule ^declinare/([^/]+)/? declinare.php?cuvant=$1 [L,NC,QSA]
RewriteRule ^conjugare/([^/]+)/? conjugare.php?cuvant=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]*)/?$ definitie.php?forma=$1&cuvant=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ definitie.php?forma=$1&cuvant=$2&pagina=$3 [L,QSA]
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$ [OR]
RewriteCond %{HTTP_HOST} ^www\.toatecuvintele\.ro$
RewriteRule ^index\.php$ "http\:\/\/www\.toatecuvintele\.ro\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^toatecuvintele.ro$
RewriteRule ^/?$ "http\:\/\/www\.toatecuvintele\.ro\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^37\.156\.32\.57
RewriteRule (.*) http://www.toatecuvintele.ro/$1 [R=301,L]
Everything works fine except for one thing:
http://toatecuvintele.ro/cuvant/test does not redirect automatically to http://www.toatecuvintele.ro/cuvant/test
How can I do to redirect everything withc is Non WWW to www?
Thank you.
You can do that:
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$
RewriteRule ^/?(.*) http://www.toatecuvintele.ro/$1" [R=301,L]
Have you tried that:
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$ [OR]
RewriteCond %{HTTP_HOST} ^www\.toatecuvintele\.ro$
RewriteRule ^(.*)$ http://www.toatecuvintele.ro$1" [R=301,L]

redirection all https requests to http

Small problem here, before changing my site to CMS, I have encountered a problem with redirecting all https:// requests to http://.
I have had the following in my .htaccess, which was doing all that
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
but after changing it to CMS that requires different setup, I can no longer use the above mentioned code, since it does not do what it was intended to do in the first place.
Here is my current setup from .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} !^(www\.)mysite\.net$ [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.net/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]
Can you please advise me on how to do it all correctly, since I really do not want to double up all site content because of this problem and would rather redirect it all to the one consistent protocol.
thanks in advance
Have your .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.mysite.net%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.net/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php/$1 [L]