.htaccess not working on ip/~username - apache

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]

Related

.htaccess 301 redirect with exclusion does not work

I try to use a simple 301 redirect
from domain1.com/folder/ to domain2.com/
but excluding domain1.com/folder/subfolder
I use the following code in .htaccess:
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
but it simply redirects all the requests, including the requests to subfolder.
Please, help to fix the line to make it work as described. Thank you!
here is the complete code of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
RedirectMatch 301 ^/folder/((?!subfolder).*)$ https://domain2.com/$1
Try it like this using mod_rewrite instead:
(NB: This assumes the .htaccess file is located in the document root.)
# /.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule ^folder/(.*) https://domain2.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
It is important that the redirect goes before the rewrite to your front-controller.
You will need to ensure your browser cache is cleared before testing and test with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
Yes, /folder has it's own .htaccess (this is the file I am working at all this time). Yes, /folder is where Wordpress is installed.
In that case you would need to change the above redirect to read as follows (it won't do anything otherwise):
# /folder/.htaccess
# Redirect all direct requests, except "subfolder"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond $1 !^subfolder($|/)
RewriteRule (.*) https://domain2.com/$1 [R=301,L]
Basically, you need to remove folder/ from the start of the regex that matches the URL-path. The URL-path that the RewriteRule pattern matches against is relative to the directory that contains the .htaccess file.
The addition of the check against the REDIRECT_STATUS env var is to ensure that rewritten requests to the WP front-controller (when subfolder is requested) are not redirected.
You can also "simplify" the WordPress directives that follow (although if these are enclosed in # BEGIN WordPress / # END WordPress comment markers then you should leave the directives as they are since they are maintained by WordPress). For example:
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The RewriteBase directive is not required. And neither is the <IfModule> wrapper. (But as I said above, only change this if you are hand-coding the .htaccess and not letting WordPress maintain it.)

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

htaccess redirect subfolder to root

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.

.htaccess: rewrite .htm urls internally to .php, but also redirect .php urls to .htm

I have a php site. For all of the page links I use foo.htm, and internally rewrite this to foo.php with .htaccess:
RewriteRule ^(.*)\.htm$ $1.php [NC,L]
This works great - however, it still allows you to use the foo.php url. I would like to 301 redirect foo.php to foo.htm to prevent any old foo.php search engine results from hanging around and rewrite the foo.htm url internally to foo.php
I can't figure out how to do this without creating a loop.
Put this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s(.+)\.php [NC]
RewriteRule ^ %1.htm [R=301,L]
RewriteRule ^(.*)\.htm$ /$1.php [L,NC]
RewriteRule ^(.*)\.htm$ $1.php [NC,L]
RewriteRule ^(.*)\.php$ $1.htm [NC,R]
the option L stops the rewriting engine, preventing a loop.

redirect into subdirectory AND out of subdirectory

i have a mod_rewrite redirection problem i cannot figure out.
all requests from a specific domain get "silently" rewritten into a designated subdirectory. e.g. www.mydomain.net/hello.html retrieves the file in /net/hello.html. the following .htaccess (placed in my hosting root) achieves this perfectly:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]
however, direct URLs into this directory however should visibly redirect with a 301 to the URL without that subdirectory. e.g. www.mydomain.net/net/hello.html should redirect to www.mydomain.net/hello.html (which than still retrieves the file in /net/hello.html). my .htacces file for this (placed in /net) unfortunately doesn't work:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]
i get an infinitive redirect loop despite the RewriteCond %{ENV:REDIRECT_STATUS} 200 block in the root .htaccess file... so what's wrong?
btw, i have to use mod_rewrite, because the site is externaly hosted and i have no access to the apache configs.
many thanks for any pointers.
Inspect the HTTP request line in THE_REQUEST instead:
RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]