htaccess rewrite trouble with "fake" folder - .htaccess

Before I ask my question, let me start off by saying that I have a working htaccess rule that is:
#1.
RewriteCond %{THE_REQUEST} ^GET\ /folder1/
RewriteRule ^folder1/(.*) /good/$1 [L,R=301]
and, like I said, this works well.
But, when I try to do this:
#2.
RewriteCond %{THE_REQUEST} ^GET\ /folder1/
RewriteRule ^folder1/(.*) /funfolder/good/$1 [L,R=301]
this doesn't work anymore.
So here comes my question.
If I want to rewrite the following:
#3.
http://www.mydomain/funfolder/folder2/sheet to
http://www.mydomain/funfolder/fakefolder3/sheet
when I write:
#4.
RewriteRule ^funfolder/folder2/sheet$ /funfolder/fakefolder3/sheet?&%{QUERY_STRING}
or
#5.
RewriteCond %{THE_REQUEST} ^GET\ /funfolder/folder2/
RewriteRule ^funfolder/folder2/(.*) /funfolder/fakefolder3/$1 [L,R=301]
it is not working. I do not see the content i want to see.
If I
#6.
Redirect 301 /funfolder/folder2 http://www.mydomain/funfolder/fakefolder3
The redirect works but not the content.
I don't know if it helps but funfolder isn't a real folder. Neither are folder2 or fakefolder3. The CMS (joomla), however, generates the folder2 and funfolder. but I want to add the fakefolder3.
I also though of moving the order of the above rules since there are other rewrite rules. But, it did not produce any difference.
what I don't understand, is why the #1 works and the #2 doesn't. I'm thinking that if I can understand the reason, it might also give me the answer to how to do #3. with a slithly altered #4 or altered a #5
Then I can always add a #6 if I need to.
this make any sence? anyone know why I can't get #2 to work (and thus, my real problem), how to get #3 to work trough #4 or #5?

Use the power of RedirectMatch! So your redirect will be:
RedirectMatch 301 /funfolder/folder2/(.*) /funfolder/fakefolder3/$1

I have part of an answer to my question, although it is not totally what I intended. I wanted an answer with htaccess, which I did not find, but I found a solution within the CMS.
I created an invisible menu pointing to the working link and then I used that new invisible menu item and pointed a new visible menu item on it. That had the results I wanted/needed. The new visible link (alias) was made to fit the need.
I'm still looking for a solution with pure htaccess..... but till I find it, at least I have somehting that works.

Related

How can I convert a folder into a sub domain?

I have a website that looks like this (the Ferrari is not a physical folder is an alias virtual):
www.example.com/en/ferrari
and I would like to convert this site into a sub-domain, like so:
ferrari.example.com
or something like:
ferrari.example.com/en/
The idea is to have all sub-pages from the Ferrari section working inside the sub-domain.
I don't know if this is even possible, all my .htaccess test didn't worked out.
I am searching for a re-write rule but I don't seem to find it.
EDIT:
Let me rephrase the question, I don't want to REDIRECT from example.com/ferrari to ferrari.example.com
What I want is to show the example.com/ferrari site when someone opens ferrari.example.com. That is the difficult part. If someone opens ford.example.com, I want to show all the pages that are inside example.com/ford.
You can use that in your root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ferrari\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/en/ferraricars [NC]
RewriteRule ^(?:en/)?(.*) en/ferraricars/$1 [NC,L]
Work with ferrari.example.com/en/other and ferrari.example.com/other -> example.com/en/ferraricars/other
I had faved this link a year ago and it worked for me.
PS: I don't remember, and I'm not sure, but I think that I have added some other flags in the htaccess. Let me know if it not working on comments.
With directadmin it would be possible with creating a subdomain + hatcc redirect

Make URLs pretty with one line of htaccess

So I've had this issue for some time and originally I just dealt with it the long way around but that didn't work so I was hoping for a more solid solution.
My problem currently is that using the following code:
RewriteRule ^error/([a-zA-Z0-9-]+)/$ error.php?id=$1
error.php?id=404 becomes this:
/error/404/
Which is perfect. However if I add a new page which uses an id at the end, I have to add it in to my htaccess which is extra work and doesn't seem very good in terms of best practice.
I was hoping with one line I could have all sorts of combinations but it doesn't work.
I tried the following which resulted in a default 300 multiple request error.
RewriteRule ^/([a-zA-Z0-9-]+)/$ $1.php?id=$2
Any help please?
You can use this
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1.php?id=$2 [L]
Example: /error/404/ will be rewritten to /error.php?id=404 if error.php exists
Note: maybe you'll have to disable MultiViews option (apache content negociation)
Options -MultiViews

Trying to redirect from domain.com/user/12345 to domain.com/user?id=12345

I'm trying to redirect from domain.com/user/12345 to domain.com/user?id=12345 and from domain.com/user/12345/profile to domain.com/user/profile?id=12345.
I've written this rule in .htaccess:
RewriteRule ^user/([0-9]*)(/.*)?$ user$2?id=$1 [L,QSA]
It works ok for domain.com/user/12345/profile but domain.com/user/12345 is not redirected.
I've also tried with this simplest form for this case:
RewriteRule ^user/([0-9]*)$ user?id=$1 [L,QSA]
I've tested both in http://htaccess.madewithlove.be/ and seems to work fine.
¿What is my mistake? ¿How can I do it?
UPDATE:
The next rule I've in this file rewrites from domain.com/user to domain.com/user.php and so on. If the previous rule is not defined it rewrites ok from domain.com/user/12345 to domain.com/user/12345.php but with the previous rule defined it neither does this rewriting.
Does it means that there is any kind or transformation that skip the second rule or that there is any kind of misyake that stops rules verification?
I've found the solution. I don't know why it worked well in a case and not in the other but I'd forgotten the slash so it was trying to rewrite to user?id=12345and not to /user?id=12345.
So the rule must be like this one:
RewriteRule ^user/([0-9]*)(/.*)?$ /user$2?id=$1 [L,QSA]

Why does this htaccess work for a bit, and then doesnt?

I'm trying to redirect people to a new url of the login page,
It used to work, I made a few changes though, which kind of failed as I'm still trying to learn how to use htaccess,
Anyhow I brought the very first code back, and for some awkward reason, it won't work anymore,
Here's the code:
# external redirect using R=301 to /login from /index.php?act=Login
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php/?|)\?act=Login(&|\s) [NC]
RewriteRule ^/login? [R=301,L]
Is there any better way to achieve what I need?
Thanks!
Check the regular expression in the RewriteCond line. There is (index\.php/?|). The pipe character is either to much and should be removed or you missed the second option for the sub-pattern.
Alternatively you should check whether a simple Redirect instruction will be enough instead of using complex rewrite rule for a possibly simple task.
Basic syntax of redirect is:
Redirect [status] <old-url> <new-url>
for your purposes the following should work:
Redirect 301 /index.php?act=Login /login

Subdomain redirect to URL Parameter while keeping other url params intact

I've looked like crazy for an answer to this - and I haven't been able to find one as of yet. My apologies in advance if I've missed something.
I'm looking to convert a subdomain to a url parameter while maintaining the other url pieces.
Such as:
http://sub.domain.tv/value redirects to http://domain.tv/value?campus=sub
This is what I've tried after looking through numerous posts on here:
# campus
RewriteCond %{HTTP_HOST} ^sub.domain.tv$ [NC]
RewriteRule ^(.*)$ http://domain.tv/$1/?campus=sub [R=301,L]
which worked fine for redirecting sub.domain.tv to domain.tv?campus=sub, but does not work with the rest of the URL parameters (so heading to sub.domain.tv/value does nothing).
I've tried other examples with the %1 and $1 being used as variables, such as with this post:
htaccess subdomain redirct with last url parameter but I don't understand the difference (and the examples I've tried did not work).
Thanks for taking a look! :) I very much appreciate it!
Have you tried the [QSA] parameter. This will add any remaining Query parameters to campus=sub

Resources