htaccess redirect loop on joomla - php

I have all of my customers sites in a directory on my subdomain:
customers.example.com/sites/customer_name
I have rewritten the url from:
customers.example.com/sites/customer_name
to:
customers.example.com/customer_name
the problem is when i go to a joomla site, it makes a redirect loop, resulting in this address:
http://customers.example.com/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/sites/customername//
my redirect works fine on static pages
Here is my current .htaccess file in the root of my domain:
EDIT:
found a better htaccess script which is faster, but stil gives me the same issue
RewriteEngine On
ErrorDocument 404 http://example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sites/$1/ [L]

Because joomla is probably doing routing, the two condition checks !-f and !-d will fail because joomla needs to route. Try changing the conditions to:
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^(.*)$ /sites/$1 [L]

Change L to NL on second line.

Related

Redirect a page from one domain to another domain (both domains belong to one website)

At first glanse question is pretty common, however other answers I have seen do not work in my case.
I need to redirect a page from one domain to another domain. Both domains have one web-site under them and one common .htaccess file.
www.olddomain.com/guides -> www.newdomain.com
I use .htaccess file. If someone knows answer for other way - you are welcome also.
My current file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
I have tried this way:
RewriteEngine on
Redirect 301 http://www.olddomain.com/guides https://www.newdomain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Also I tried this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://www.olddomain.com/guides$ https://www.newdomain.com [R=301]
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Does not work. How it can be done properly?
I believe your Redirect 301 line is the way to go, but should read something like this:
Redirect 301 /guides https://www.newdomain.com
See here for more information.
Edit to add:
The above will redirect to https://www.newdomain.com/
If you want a certain path, then just put
Redirect 301 /guides https://www.newdomain.com/foo
where foo is the path on the server you want to end up at.
I end up with the following.
Unlinked "olddomain" name from the origin site.
Created new website folder at my hosting with only one file .htaccess in a root that contains only one line as #CBaish suggested:
Redirect 301 /guides https://www.newdomain.com
Linked "olddomain" to this folder
Now all traffic from the page "/guides" of olddomain will go to my newdomain.

htaccess rewrite subdirectoy to file

I have a problem with rewriting urls to my files. What I am trying to do is making my little shop system a bit more SEO friendly. My problem is that it sometimes works and sometimes it doesn't. I have no idea what I should do or what I am doing wrong.
My UPDATED .htaccess file:
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkte/?(.*)$ products.php$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkt/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
For example the /shop/products link is not working but /shop/products/ is.
And for some reason if I want to open the link /shop/products?cat=besteck its redirecting me to: localhost/D:/xampp/htdocs/shop/products.php?cat=besteck but If I capitalize the b it's working fine..
I have no Idea what to do, please help me! (And dont just give a working code snippet explain why mine fails and yours works)
EDIT:
Just to clear things up I want /products, /products/ and /products?some_get_query to redirect to my products.php file. /product/some_seo_url should be redirected to product.php?url=some_seo_url. I tried adding a question mark after the forward slash in my RewriteRule and I also tried putting the ^products rule above the ^product rule. Nothing worked yet.
EDIT 2:
I updated my .htaccess code above and now nearly everything works. The only thing that still doesn't work is when I open /shop/products/?cat=fish or /shop/product/some_product, my resources aren't loading!
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/?(.*)$ products.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
First off your product rule also matches products so a rewrite like:
products/cat/fish becomes product.php?url=s/cat/fish
Which is not what you want, the easiest way to avoid that is to reverse the order so that the products rewrite comes before the product one but I've also added the Last flag ([L]) to be on the safe side; besides, once it's got the match you want it's better for it to stop looking.
To prevent recursive rewrite loops you need to specify that the rewrite only occurs when the redirect is not an existing file or directory (otherwise your product rewrite matches product.php and it loops - forever). That's what those RewriteCond lines signify.
Other than that it seems OK.

.htaccess doesn't redirect direct link to .php files

This is my current .htaccess file:
DirectoryIndex requestHandler.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ requestHandler.php?/$0 [L,QSA]
I want to redirect alle requests to "requestHandler.php". It works now, but it is also possible to access sites with the direct link, and I don't want that.
For example:
It now works with ".../api/register" , but you can also access it but going to ".../register.php" and that shouldn't be possible. It should only be possible to go to register.php by ".../api/register".
I think I had it working before, but as I continued editing I've seemed to mess it up.
requestHandler.php should be working properly and when I enter ".../register.php" it is not at all redirected to requestHandler, but if I enter ".../registe.php" or ".../register.ph" it is redirected there.
Any solutions?
The rule you posted is only checking if the file/directory exists, and if it doesn't, then redirect to requestHandler.php. So naturally, if an existing file is entered in the URL, it will not redirect.
If you want all ".php" files to get redirected as well, you'll need a more specific Rewrite rules. Something like this maybe:
DirectoryIndex requestHandler.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ requestHandler.php?/$0 [L,QSA]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^(.*)$ requestHandler.php?/$0 [L,QSA]
You can probably consolidate that into one rule block, but at least this way it's very readable. The second rule block is only checked if the requested file is not a file or a directory.

Modified .htaccess to hide index.php for a routing engine cause to 404 error

I've created a custom simple routing engine which works base on paths in 'PATH_INFO' in $_SERVER. If the rout matches with any predefined routs, it will call its controller action; otherwise it will redirect to the home page.
$path = $_SERVER['PATH_INFO'];
if (in_array($path, $routs)) {
call_user_func(array($this, $action));// $action is calculated
}
else {
header('location: ./en');
exit();
}
In this case I modified the .htaccess file as below to hide the index.php and my urls be like http://www.example.com/en or http://www.example.com/en/home
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [QSA,L]
When I deployed sources to my domain by hitting url http://www.example.com redirects to http://www.example.com/en which is correct; but in response for that redirect it returns a 404 Page not found page!
Everything works fine in my machine localhost but on the web it does not work. The only different on my machine is that in .htaccess RewriteBase is set to my project path as RewriteBase /myrouting/.
If I go to http://www.example.com/index.php/en on the web it works fine; but I don't want the index.php be visible.
UPDATE
I changed my .htaccess as below; now if I hit www.example.com/index.php/en it redirects to www.example.com/en but still with 404 error
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
I will be thankful if anybody could help me what I've done wrong. I did search and tried different changes on .htaccess with no success.

Mod Rewrite ignore url

I am creating a site where instead of using GET variables I will just be looking at the url. How would you create a mod_rewrite rule that no matter what directs the user to index.php(Or some page)?
EX:
User enters: www.example.com/blog/programming/postName
User still sees www.example.com/blog/programming/postName in the adress bar but www.example.com/index.php is shown
I have tried:
RewriteRule ^([a-z]+.)?$ /index.php [NC,L]
But that only changes the page for one directory(only worked for www.example.com/worksNow/
RewriteRule ^(.*) /Blog3/index.php [NC,L]
But It got a server error
This behavior/recipe doesn't require mod_rewrite, see FallBackResource.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
This means if any request that is not for a file or directory then internally rewrite that to /index.php
Reference: Apache mod_rewrite Introduction

Categories