htaccess rewrite if url doesnt have a query string - apache

I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.

With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

How to htaccess url rewrite multi language

Can someone help me with the htaccess rewrite url?
I need to convert ?lang=en to /en
To always display the language used in the url
When someone comes to example.com to automatically 301 redirect them
to example.com/en as the main language.
When a user chooses another language to stay on the same page e.g.
example.com/en/contact.php
I have a total of three languages on the site en, de, hr.
The problem with my code is when I go to example.com/contact and select a language, it takes me back to the homepage example.com/en. If I type manually example.com/en/contact it works.
If I am at example.com/en/contact and choose another language e.g. /de url changes to example.com/en/de and a 404 error.
My current htaccess code looks like this
# Convert ?lang=en in /en
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
RewriteRule ^([bs|en|de]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
# Remove trailing slash from URL
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Remove php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !ON
RewriteCond %{HTTP_HOST} ^(?:www\.)reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\scontact/contact\.php [NC]
RewriteRule ^ https://reg.bmi.id [R=301]
RewriteRule ^ contact/contact.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Joomla: Permanently redirect from example.com/index.php/foo/bar to example.com/foo/bar (no index.php in URL)

.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
With this setup the page /foo/bar is accessible under the pretty URL example.com/foo/bar while the ugly URL example.com/index.php/foo/bar is still valid. What I need to achieve is a permanent redirection from example.com/index.php/foo/bar to example.com/foo/bar.
RewriteRule .* index.php [R=301,L] doesn't work.
RewriteRule (.*) index.php/$1 [R=301,L] does the exact opposite, it redirects from example.com/foo/bar to example.com/index.php/foo/bar. Please help me out!
I recommend writing this:
RewriteEngine On
RewriteBase /
# remove index.php from / OR any /dir
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# redirect rule to remove /index.php/ from start
RewriteRule ^index\.php(/.*) $1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !^/index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
In the new rule, we match a pattern using regex ^index\.php(/.*) which matches a URI that starts with /index.php/ followed by zero or more of any characters. (/.*) is our match and the capture group after /index.php which is used as back-reference i.e. $1 (string that we've captured in group #1) in target to get a URI after /index.php as desired.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks

.htaccess remove .html and force redirect to extensionless url

I'm trying to remove .html extensions from URLs and to have 302 redirection to the extensionless URLs like this:
http://example.com/file.html -> http://example.com/file
I have looked at so many stackoverflow answers, tried them but unfortunately no success. I came out with this and I don't understand why it does not work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteRule ^(.*)$ $1.html
Where the first block should redirect to the extensionless version and the second block should find the file.
You need to use %{THE_REQUEST} here to prevent rewrite loop error. otherwise without THE_REQUEST variable Rule keeps rewriting file => file.html=>file=>file.html..
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule (.*) /%1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Redirect not working for complex rewrite rules (.htaccess)

I'm trying to make a redirect from a non-www version of the link to the www one. Its working fine for something like http://mywebsite.com but it fails for a request like http://mywebsite.com/artists/metallica/ or even a complex one. The whole .htaccess file is bellow. Any clues?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]
RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L]
RewriteRule ^submit/$ /submit.php [QSA,L]
RewriteRule ^users/$ /users.php [QSA,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But make sure that you put this rule in front of those rules that just do an internal rewrite. Otherwise an already internally rewritten rule might get redirected externally.