mod_rewrite: Redirect everything to / (docroot) - apache

I have a site which has only ONE file: index.html.
I just want to redirect everything except for http://www.mysite.com/ to http://www.mysite.com/.
That includes /index.html, somephonydir/, somefile.txt, you name it -- I don't care what it is, I just want to redirect to /.
I've tried the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^.*$ / [L]
This gives me a 500 Internal Server error.

I found the answer. I guess I forgot to actually tell it to redirect. ;)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^.*$ / [R=302,L]

RewriteRule ^/(.+)$ /

Related

How to remove last part of my URL with .htaccess?

Currently I have an URL that looks like :
www.mywebsite.com/confirm/ZChCQhXNJ5i
The last part of the URL is a random string. I want to remove it from my URL to get something like that
www.mywebsite.com/confirm
I've tried many things such as RewriteRule ^(confirm)/.+$ /$1 [L,NC] but I really don't understand the .htaccess syntax.
Edit 1 - Here is my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule ^(confirm)/.$ /$1 [L,NC]
</IfModule>
Edit 2 - Your answers helped me, here is the solution I found :
RewriteRule ^(confirm)/(.*) /$1 [L,R=301]
Have you tried using this RewriteRule?
RewriteEngine On
RewriteRule ^(.*)/ZChCQhXNJ5i /$1 [L,R=301]
When I test this it works perfectly. See here: http://htaccess.mwl.be?share=33ae279c-7597-5935-8bf0-d389add54d0a
It successfully changes your URL to www.mywebsite.com/confirm using a permanent 301 redirection.

RewriteRule: match only first occurance of a string. Match /mystring but not /some/path/to/mystring

I have the following RewriteRule in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^mystringtoreplace/?$ http://www.example.com/some/other/path/mystringtoreplace [L]
Intention is to redirect http://www.example.com/mystringtoreplace to http://www.example.com/some/other/path/mystringtoreplace.
What I get is a too many redirects error "ERR_TOO_MANY_REDIRECTS".
How can I write a correct RewriteRule for that problem?
In fact the RewriteRule is correct. There was a problem with WordPress Permalink. See comment above.

changing url with get paramater in .htaccess not working

I am trying to change my url using htaccess but it doesn't seem to be working. I want http://example.com/blog_view?id=1 to be changed to http://example.com/blog-id/1/ but it isn't working. I have tried using this.
RewriteEngine On
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You might have to add a RewriteBase directive:
RewriteEngine On
RewriteBase /
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You can test your rules with this tool
You need one redirect and one rewrite rule (already existing):
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog_view\?id=([^\s&]+) [NC]
RewriteRule ^ /blog-id/%1? [R=302,L,NE]
RewriteRule ^blog-id/([^/]+)/?$ blog_view?id=$1 [L,QSA,NC]

.htaccess rewrite for a specific subfolder

My goal:
every url http://www.domain.tld must be served by http://www.domain.tld/www/frontend/web
AND
every url http://www.domain.tld/backend must be server by http://www.domain.tld/www/backend/web
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/backend
RewriteRule ^backend(.*)$ /www/backend/web/$1 [L]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ /www/frontend/web/$1 [L]
</IfModule>
The first goal is ok, but the second no.
The server redirect my /backend to /www/frontend/web/backend/.
I tested even it using an htaccess tester online and it give me the same results.
For precision, even if i used [L], the first rewrite rule is ignored or, at minimum, the execution is not stopped.
You need to exclude rewritten backend URI from 2nd rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^backend/(.*)$ www/backend/web/$1 [L,NC]
RewriteCond %{REQUEST_URI} !^/(public|www/(backend|frontend)/) [NC]
RewriteRule ^(.*)$ www/frontend/web/$1 [L]
</IfModule>
It turned out to be a weird server configuration. After going through pretty much every other method of checking for SSL including but not exclusively:
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{SERVER_PORT} !=443
The only one that worked on the clients host was:
RewriteCond %{ENV:HTTPS} !=on
Thanks to #anubhava for pointing me in the right direction. Hopefully this will save someone else wasting half a day of their lives.

HTACCESS - Applying some simple mod_rewrite rules

this is urgent a lot. I need help with some htaccess rules. The clue is when user receives an answer for sth the real url will be www.mydomain.com/results.html/sth but he must can see in his browser only www.mydomain.com/sth only and when i redirect him to home page (www.mydomain.com/index.html) the url has to be www.mydomain.com only.
Thanks
#this is for the www.yourdomain.com/index.html
RewriteRule ^$ index.html [L]
RewriteRule ^/$ index.html [L]
#this is for the results
RewriteRule ^([^/]+)$ results.html?$1
RewriteRule ^([^/]+)/$ results.html?$1
Replace the first 3 lines of code with:
DirectoryIndex index.html
it should have worked by default though
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(results|index)\.html
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^(.*)$ /results.html/$1
RewriteRule ^index\.html$ / [R=301,L]