.htaccess rewriterule problems - .htaccess

I need a few redirects in my .htaccess file, and what I've done works perfectly well in the htaccess tester, but it doesn't work at all on my server. I've spent days finding a solution, and I'm stuck. It would be wonderful if someone could help!
1) "/-" in URLs
This is a good example:
https://www.haraldjoergens.com/galleries/remembrance-sunday/2012-cenotaph/-march-past/index.php?page=40
The "2012-cenotaph/-march-past" is supposed to be "2012-cenotaph-march-past"
I had though a straightforward RewriteRule ^(.*)/-(.*)$ %1-%2 [R=301,L] would do the trick, but it seems to be completely ignored.
2) Repetition needs to be removed:
Due to making the "Replace" a 301 replace too early, the search engine bots try to access very wrong things:
https://www.haraldjoergens.com/galleries/rowing/index.php/galleries/rowing/
The second "/galleries/rowing" needs to be removed. My attempt via
RewriteRule ^(.+/index\.php)/.*$ /$1 [R,L]
works fine in htaccess tester, but seems to send the website into an endless loop.
Any suggestions would be really appreciated!
Thanks!!!
Harald

htaccess example
I dont know if this is what you really need.
One thing helps me is to change the routes for example:
$route['index.php/galleries/rowing/'] = "index.php";

Related

Internal rewrite to remove directory from url with htaccess

I've searched and tried many tutorials and problem solutions but couldn't achieve what I want.
If user open domain.com/xxx.php he see the content of domain.com/hosting/xxx.php
This
RewriteRule ^xxx.php$ /hosting/xxx.php
Works perfectly what i want to do, but there are over 150 pages in hosting directory and writing rules for each page does not seem right? any possible way to do it dynamically?
I tried this solution it works but it try to find those pages which are currently at root domain.com/xxx.php inside domain.com/hosting/
I search slimier questions have been asked many times before but non of the answer worked for me.
You need to also take care of reuests to the shortened, rewritten URL:
RewriteEngine on
RewriteRule ^/?hosting/(.*)$ /$1 [END,NC,R=302]
RewriteRule ^/?(.*)$ /hosting/$1 [END]

.htacces redirect from domain 1 to domain 2 subdir, keeping path

I have found plenty of useful answers in this forum that got me to the point I'm at, however the configuration of my .htaccess redirect is still not 100% perfect.
Set-up
I have two domains registered:
domain1.is (only domain)
domain2.me (domain + hosting)
The domain1 redirects as wildcard everything to domain2.
What I'm trying to do
I have installed yourls (url shortened) on domain2.me/yourls and want to redirect all domain1.is/randomstring to domain2.me/yourls/randomstring.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.is$
RewriteRule (.*) https://domain2.me/yourls/$1 [R=301,L]
RewriteRule ^$ store [L,R=301]
The issue
So far I got that domain1.is/randomstring goes to domain2.me/randomstring, however the subdirectory in the .htacess file seems to be completely ignored, effectively rendering the whole system unusable.
I've tried adding a condition, changing the subdirectory, etc. but can't seem to find the reason why. Can anyone help please?
Thanks in advance and cheers from Berlin!
I'll share my solution in case someone else stumbles upon this conversation. The details/history are in the comments of the question.
TLDR;
Just set up a wildcard redirect to the subdirectory.
(not sure if the first redirect is useful, but still).
Important point: you don't even need to touch the .htaccess file (in my particular case yourls already had one, so I left it as it is).
Credit: thanks #CBroe for pointing me in the right direction.

I am not sure if my .htaccess is wrong or if the server didn't read my .htaccess file yet

I am pretty certain, that my code is alright, for a briefly moment, my htaccess seems to work when i entered an url. After I extended the file and reuploaded it, my Rule didn't work any more. Even when I removed my edit the Rewrite Rule don't triggert any more.
Did i miss something in my Rule. I don't get it whats wrong right now.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^www\.sub\.domain\.de\/(.*)\.html$ www.sub.domain.de/typo3/$1.html?&%{QUERY_STRING}
</IfModule>
Okay. My first mistake was the complete url as first param in RewriteRule.
After I followed Kaddath's instuctions the rewrite kinda worked. I still had a problem with a loop call. The htaccess appendes planty "typo3/typo3/...", so that a RewriteCondition has to solve this. I checked if the url has already typo3 in his string. Now the Url is correctly rewritten.
I also tried [L] before to stop a looping rewriteRule, but it didn't help.
Thank you Kaddath for giving the right clues.

htaccess: rewrite to optional amount of "subfolders" www.xyz.com/a/b/c/d

sorry for my bad english and wrong use of technical terms.
So I got these links on my site:
www.xyz.com/index.php?link=abc
I want to rewrite via htaccess like this:
www.xyz.com/def/.../jki/abc
/.../ meaning that I want to be able to be as flexible with my "link-design" as I want.
I tried it this way:
RewriteRule ^.+/([a-z-.-?]+) index.php?link=$1 [NC,L]
and it works, but the strange part is, that it doesnt work all the time, or it totally stops working. Sometimes images and css is not being loaded anymore.
Then I switched to test it with
RewriteRule ^.+/m/([a-z-.-?]+) index.php?link=$1 [NC,L]
and this works fine, but I'm abviously stuck with the /m/-part like:
www.xyz.com/def/.../jki/m/abc
Is there even any option to do this?
Thank you very much.

The requested URL /public_html/ was not found on this server

I'm working on a local MAMP website. I use a micro MVC framework to use friendly urls
so I don't need to call index.php (which is inside of public_html directory) in the urls.
To achieve that, I have the following htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public_html/
RewriteRule (.*) /public_html/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Then I have the following line in /etc/hosts
127.0.0.1 mywebsite
Also, I have the following in httpd.conf
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/mywebsite"
ServerName mywebsite
</VirtualHost>
So if I simply call http://mywebsite from the browser, the whole thing works smoothly.
So what's the problem?
The problem appears if I try to reach the same page from another machine in my LAN.
So if I write http://192.168.1.15/mywebsite the answer is:
Not Found
The requested URL /public_html/ was not found on this server.
I get the same message if I call http://localhost/mywebsite from my own machine.
I have the feeling that is something related to .htaccess, but I've been trying a
lot of different ideas I've found in the web, and nothing works.
I'd like to fix this, because I need other people to check the website from their machines.
If you have any clue please help. Thanks a lot.
Edit: I can't solve this, so as a temporary fix I've created a free account at AppFog for my team to be able to access the page until we go to production.
After all these years, I thought that computing would be easier... it's getting harder, actually. The htaccess file is a huge mistery to me! Thanks anyway :)
By the way... as I told you, I've found a way to fix the problem. The funny thing is I have to use a different .htaccess file. I thought you could be interested, provided that it seems you like computing stuff :)
This is the .htaccess that works in my LAN debian server:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(www/.*)$
RewriteRule ^(.*)$ index.php [QSA,L]
As you can see, it's different from the one I showed you at my first post. My conclusion? Well, even when I always try to keep things simple, life shows me one and another time that everything can be more complicated. Look at the .htaccess file. It's so funny... I've learnt to work with it by using different combinations of code.
Isn't it crazy? Yes it is. Please let's do understandable software. Thank you!
Ok, I've been trying a lot of different things and all of them fail. The only solution I've found is to install the web site in a separated LAN debian server. Everything works ok in that way, but it's not possible to make it work from my machine with MAMP.
Why not? Well, I don't know. After a lot of years in computing, I've learnt to say "I donĀ“t know", you know what I mean.
I've been working for a few days with an AppFog account for free but, you know, that thing cannot work when you put a database and the whole thing there. Obvious.
So at the end the only solution I've found has been to put everything at that local LAN debian server.
Thank you anyway :)

Resources