htaccess redirect subfolder to root - apache

This has been frustrating me and I can't find the same issue, maybe I am just searching wrong. Someone messed up and we have a bunch of Google links to a subfolder that does not exist. So I am trying to do a 301 redirect in htaccess to sort that out.
The problem is I can't seem to get the redirect to work more than one folder deep.
Example. http://example.com/subfolder/someOtherFolder redirects to http://example.com/someOtherFolder just fine.
However http://example.com/subfolder/someOtherFolder/yetAnother stays on the same page and returns a 404.
This is the last variation of my entire .htaccess that returns the above results, nothing I've tried has returned anything but the above.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^sitefiles/(.*)/$ /$1 [R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
Help? (I hate .htaccess >.<)

The problem is with this rule:
RewriteRule ^sitefiles/(.*)/$ /$1 [R=301]
This rule required a trailing slash on the requested URI
Instead:
RewriteRule ^sitefiles/(.*) /$1 [R=301]
Note that there's also a Redirect directive to do simple Redirects. In your case:
Redirect /sitefiles /
Finally, note that the entirety of that Rewrite block can be replaced with:
FallbackResource /index.php
Thus, the final recommended (best practice) configuration would be:
Redirect /sitefiles /
FallbackResource /index.php
Rather than using Rewrite at all.

Related

htaccess redirect phpbb to subdirectory

I need help with a redirect using htaccess since I moved my phpbb forum to a subfolder called "forum". So I want viewtopic.php?... to redirect to /forum/viewtopic.php?...
and viewforum.php?... to /forum/viewforum.php?...
I google all day and couldn't find an exact code to use so I tried to fiddle with the code to no avail.
RewriteCond %{REQUEST_URI} !forum/
RewriteRule ^([view(.+)\.php(.+)])$ forum/$1 [QSA,NC,L,R=301]
I tried to catch both "viewtopic" and "viewforum" and then redirect it to /forum/ but it's not working.
This should work for you:
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^view(.+)\.php$ /forum/$0 [QSA,NC,L,R]
Because of the nature of the redirect (with the need to capture the entire request URI), there is no need to wrap it - you can just us $0.
You were using square brackets in your capture, which which would not have helped in any way. Square brackets indicate a character set.
If the new rule works for you, change the R flag to R=301 (as you had it previously), which will make the redirect permanent.
Update: Your entire .htaccess file should look like this now:
Options All -Indexes
RewriteEngine On
RewriteRule ^$ - [E=noabort:1]
# Redirect Forum
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^view(.+)\.php$ /forum/$0 [QSA,NC,L,R]
# WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The redirect rule must come before the WordPress rules.

.htaccess not working on ip/~username

Here is my case. I'm trying to use .htaccess for rewrite url, but no matter how I try, browser always show 500 error or 404 error.
here is my code.
RewriteEngine On
RewriteBase /~torinots/beta/
RewriteCond %{REQUEST_FILENAME} !^(/beta/home)
RewriteRule ^home$ index.php [L]
example path: http://xx.xx.xx.xx/~username/beta/
Pls advice.
Update
I found this work!
RewriteEngine On
RewriteBase /~torinots
RewriteRule ^beta/home/?$ beta/index.php [L,NC]
I assume you have the .htaccess file in /~username/beta, since you use paths relative to that in the example above. Having a simple rule like the following rule will correctly internally rewrite the url, assuming that no other rules in .htaccess files higher up are interfering.
RewriteRule ^home$ index.php [L]
If you want to redirect a request to index.php too, then you need to prevent an infinite loop from happening. You can either use the END-flag (version 2.3.9 and up) or the THE_REQUEST trick.
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [END]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteRule ^index\.php$ home [R=301,L]
or:
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [L]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ home [R=301,L]

.htaccess get lost query string

I have limited .htaccess knowledge and i am requiring some help. I need to do some redirects to enable pretty urls.
In local all works fine but it is not working in another develpment server. apparently the query string get drop when redirect.
i need to redirect this http://mysite.local/info/permalink/1
to this one http://mysite.local/info?proxy=true&id=1
my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#remove /index.php from home url and redirect to root.
#http://mysite.local/index.php -> http://mysite.local/
RewriteCond %{THE_REQUEST} ^.*\/index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L,QSA]
#pretty url without index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [PT,QSA,L]
#rewrite to handle some permalink saved on my db.
#http://mysite.local/info/permalink/1
#http://mysite.local/info/proxy=true&id=1
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$3 [L]
</IfModule>
the redirect is working but the query string is not present. When I run var_dump($_GET) on info module i am getting an empty array array(0) {}
i have try it to solve changing
RewriteRule .* index.php [PT,QSA,L]
to RewriteRule ^(.*)$ /%{QUERY_STRING} [L]
and
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [L]
to
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [QSA,NE,L]
Server API is CGI/FastCGI
What should I change to ensure that my rewrite works as intended and $_GET variables still are accessible?
thx in advance
I have no idea how you've managed to get this to work with a regex pattern like: ^([^/]+)/info if the URL you are going to is /info/permalink/1.
The ^([^/]+)/info pattern means there's stuff before the /info part of the URI, which there isn't. Additionally, in an htaccess file, the URI's have the leading forward slash stripped off. So you probably want something like this:
RewriteRule ^info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$2 [L]

.htaccess rewrite all 'other' URLs

Hopefully this is a simple one. I have a really basic .htaccess that rewrites any request to /admin (or /admin/etc) to /_admin/index.php. So far so good:
#Options All -Indexes
RewriteEngine On
RewriteBase /admin
RewriteRule ^admin/$ /_admin/index.php [QSA]
RewriteRule ^admin$ /_admin/index.php [QSA]
RewriteRule ^admin/(.+)$ /_admin/index.php [QSA]
What I also want is a generic "catch all else" rule that rewrites any other url (/users, /turnips/, /some/other/path and so forth) back to /index.php
I can't seem to get that to work - its either server error 500's or /admin also gets rewritten to the root page. I'm sure I just need to do something with RewriteCond but I can't figure it out.
Thanks!
Add this after the other rules. It would be the default rule if the previous rules are not applied.
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule .* index.php [L,QSA]
First of all I suggest you add the L flag to your rewrites so you're sure to avoid unintended matches after matching a rewrite (unless intended of course).
Secondly WordPress uses the following code to rewrite all URLs that are not matching index.php OR a file that already exists. This way files accessed directly like images, text files, downloads etc are not rewritten. Note that originally it also included the line RewriteCond %{REQUEST_FILENAME} !-d to also not rewrite directories but you seem to want that behaviour:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php - [L]
Please see if this fits your needs.

How to merge several HTAccess rewrites?

I have the following .htaccess file for my website:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /$1.php [R=301,L,QSA]
This works, in that the url 'hello' works, as well as 'hello.php'.
What I want to do, however, is the following:
Remove .php from 'hello.php' and redirect to 'hello'.
Remove (and redirect) trailing slash (if any).
How can this be done? I imagine it is quite simple.
EDIT: I was just working on this and it also has a redirect loop if you browse to 'hello.p' and if you browse to 'hello/'.
Your current rule is instructing Apache to 301 redirect to the route with the .php extension.
To prevent this, you'll need to remove the R=301 from the flag list.