.htaccess Rewrite not working without slash in the end - .htaccess

I'm with an issue with .htaccess and I hope you can help me out.
Basically I have the following structure:
root/
+ _archive
+ index.html
+ folder1
+ folder2
+ css
+ ...
On archive I have a few folders that I want it to be accessible through the root of my site.
So let's say my site calls rafael.com, so I would have the following archives:
http://www.rafael.com/_archive/folder10
http://www.rafael.com/_archive/folder20
http://www.rafael.com/_archive/folder30
I want them to be accessible from (without _archive):
http://www.rafael.com/folder10
http://www.rafael.com/folder20
http://www.rafael.com/folder30
But also having the folders and css, and images and etc on my root to keep working. Keep in mind that under folder10, folder20, folder30 I also can have their own images and css, and javascript.
Well, I'm trying the following .htaccess
RewriteEngine On
RewriteCond $1 ^(folder10|folder20|folder30)
RewriteRule ^(.*)$ _archive/$1 [L]
and that works fine if I call using http://www.rafael.com/folder30/ (WITH SLASHES IN THE END) my problem is when I try to call http://www.rafael.com/folder30 without the slashes it gets REDIRECT to http://www.rafael.com/_archive/folder30/ .
So can anyone explains to me WHY it's being redirecting it and how do I fix it in order to have http://www.rafael.com/folder30 and http://www.rafael.com/folder30/ working without redirecting it? :)
Thank you in advanced.

Do you have any other rules in place? without an [R=30x] flag it shouldn't visibly redirect the url. Also you need to fix your RewriteCond $1 to something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^folder(10|20|30)
RewriteRule ^(.*)$ /_archive/$1 [NC,L]

Related

I need to .htaccess redirect my subfolder to the root folder, except two folders

The subfolder i want to redirect to the root folder is www.example.com/subfolder
So I need to redirect everything in that subfolder to the url adress https://www.example.com except for these two folders (which are placed in the subfolder): www.example.com/subfolder/folder1 and www.example.com/subfolder/folder2
I spent like 4 hours trying to find the exact code but I could not solve that. Nothing worked for me.
I've tried many codes, but nothing worked for me. For example:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ /$1 [R=301,L]
and
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
#RewriteRule (.*) http://www.example.com/subfolder/ [R=301,QSA,L]
RewriteRule (.*) https://www.example.com [R]
Could someone help me with that?
I'd prefer to place the .htaccess file in the subfolder.
Thank you so much.
I'd say that your first attempt looks pretty good. Just make a slight modification since you say you want to redirect to the root path, not something inside the root path:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ / [R=301,QSD,L]
Since you want to use a distributed configuration file (as opposed to the preferred central configuration for the http host) that rule is meant to be used inside a ".htaccess" file inside the "subfolder".
It is a good idea to start out using a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. Also you need to make sure that you always test using a fresh anonymous browser window to prevent client side caching effects.
And you also need to make sure that such distributed configuration files are considered at all by the http server for that location (see the documentation of the AllowOverride directive).

How could I remove slash from a url in .htaccess file?

How could I remove trailing slash in front of a url? For example: stackoverflow.com/hello works on server, which definitely redirects to page named hello, but actually this is not the case on localhost.
However, it ends up something like localhost/hello instead of folder name included in it,Expected result would be localhost/stackoverflow/hello.
<a href="/hello" />
Expected output: localhost/stackoverflow/hello
Output got: localhost/hello
Actually this stuff is working on a live website but not on localhost. o I was looking for some tips to make it this way with .htaccess file.
HTACCESS FILE
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
PS: I have more than 40 files, So I don't think removing "/" from every url makes sense.

htaccess rewrite showing redundant root folder

I've searched but haven't found a solution to this exact problem.
I have a website domain 'mainwebsite.com' and several other top-level-domains managed in the htaccess. The issue I'm having is with 'alternatedomain.com' and subdirectories.
#alternatedomains
RewriteCond %{HTTP_HOST} alternatedomain.com
RewriteCond %{REQUEST_URI} !/alternatedomain/
RewriteRule ^(.*)$ alternatedomain/$1 [L]
This works fine, alternatedomain.com happily serves up mainwebsite.com/alternatedomain .
The problem is when I have access any folder like
alternatedomain.com/folder
, the URL in the title bar changes to
alternatedomain.com/alternatedomain/folder/
and it's driving me crazy. Can't figure out what I'm doing wrong!
EDIT: It only happens when I try access alternate.com/folder, and not when there is a suffix /, so alternate.com/folder/ works fine, without the redundant folder name
Your help is most appreciated!
This is because mod_dir (that adds the /) does not know the url is rewritten.
You could just disable DirectorySlash with
DirectorySlash Off
This will however make requests to alternatedomain.com/folder result in a 404 not found. You can fix this with some rewriterule though. So the complete solution would be something like:
DirectorySlash Off
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^alternatedomain/(.+[^/])$ /$1/ [R,L]

htaccess to skip a folder

I see htaccess questions all over the place but I cant seem to get the thing to do what I want. Im not sure if a redirect or a rewrite is what I need.
Im looking for a way to use a sub folder but make it look there is not a subfolder.
I have a bunch of folders in the root of my domain:
www.mysite.com/folder1
www.mysite.com/folder2
www.mysite.com/folder3
www.mysite.com/folder4
Id like to be able to move folder1 and folder2 to a subfolder but not have that folder visible to the user. For example
www.mysite.com/x/folder1
www.mysite.com/x/folder2
www.mysite.com/folder3
www.mysite.com/folder4
But the user would still see it as it originally was.
It seems like I would have to use a redirect and a rewrite. If a user goes to www.mysite.com/folder1 the will actually be in www.mysite.com/x/folder1 without knowing that they are in that subfolder.
Of course I need to do this without disrupting folder4 and folder5. Ideas? Tips? Tricks? Im not too familiar with htaccess
This is what worked for me, maybe it will help someone else:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
This is telling the server to ignore the Public folder, so when my URL is actually mysite.com/public/file it shows and responds to mysite.com/file

Cannot remove index.php from my urls in Codeigniter

I am using codeigniter and when I set up the website I added necessary code in my .htaccess file to remove index.php (default) from my urls. I just happened to replace my public_html folder from a back up file of my website. Since I did that, I cannot get any of the links on home page to work unless insert index.php in the url between the domain name and the rest of the url as in http://www.obsia.com/index.php/contact/ instead of http://www.obsia.com/contact/ and it works. So, in my config file within the application folder, I changed
$config['index_page'] = "";
to
$config['index_page'] = "index.php";
and all the links seem to work now. But how do I remove index.php from the URLs.
Here is what my .htaccess code looks like:
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|public|user_guide|robots\.txt|css)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Can anyone point me in the right direction. It will much appreciated.
Regards,
G
What yo want to do can be done this way:
RewriteEngine on
RewriteBase /
# Static content. Rewrite to - (don't change) and mark as last rule.
RewriteRule !^(index\.php|public|user_guide|robots\.txt|css) - [L]
# Do the rewrite.
RewriteRule ^(.*)$ /index.php?/$1 [L]
However, a slightly better way of doing that is this:
RewriteEngine on
RewriteBase /
# Only do the rewrite under the condition that the requested URL isn't a (real) file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?/$1 [L]
there are so many ht access files... In my case i was editing the wrong file... u should edit the file that is inside the project.. for example i work on code igniter and project name was test , so inside test folder i had a htaccess file and inside my application folder ie test/application there was another htaccess file.. i was editing the access file inside the application folder, but i had to edit the file in test folder not test/application/.htaccess.... hope sum 1 finds this useful.. cos i spent half a day to figure this out :(
Cheers
ksp

Resources