Some .htaccess questions - apache

First of all, I'm new to stackoverflow, so sorry if the title should've been more descriptive.
I've searched and tried different solutions posted here on stackoverflow but not a single one seems to solve my specific issue.
First of all, here's my .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website\.io [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.website.io/$1 [R,L]
RewriteRule ^hosting/?$ hosting.php [NC,L]
RewriteRule ^premium/?$ premium.php [NC,L]
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^auth/?$ login.php [NC,L]
RewriteRule ^contact/?$ contact.php [NC,L]
RewriteRule ^auth/forgot?$ forgot.php [NC,L]
RewriteRule ^about/?$ about.php [NC,L]
RewriteRule ^auth/activate?$ activate.php [NC,L]
RewriteRule ^website/new?$ createwebsite.php [NC,L]
RewriteRule ^profile/edit?$ editprofile.php [NC,L]
RewriteRule ^auth/logout?$ logout.php [NC,L]
RewriteRule ^privacy/?$ privacy.php [NC,L]
RewriteRule ^profile/?$ profile.php [NC,L]
RewriteRule ^auth/forgot/reset?$ resetPassword.php [NC,L]
RewriteRule ^tos/?$ tos.php [NC,L]
RewriteRule ^website/?$ websites.php [NC,L]
RewriteRule ^website/manage?$ managewebsite.php [NC,L]
RewriteRule ^website/manage/([A-Za-z0-9-]+)/?$ managewebsite.php?id=$1 [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^(.*)$ https://www.website.io%1 [R,L,NC]
My first problem is that HTTPS is not being redirected properly in some cases. For example, if I enter "website[dot]io" in the address bar, it redirects to "[https]://www.website.io", the desired result. However, if I type either "[http]://website.io" or "[http]://www.website.io" or "www.website[dot]io" it stays in the HTTP, which I don't want it to. Is there a way to ALWAYS redirect the traffic to HTTPS, regardless of the way it's typed in the address bar?
My second problem is that I want to eliminate the last trailing slash from the url in case someone decides to type it that way. Example "[https]://www.site.io/register/" should redirect to "[https]://www.site.io/register" without the last trailing slash.
Thank you so much, everyone!
P.S.: I replaced some "http(s)" with "[http(s)]" because it won't let me post links.

To remove the trailing slash use: (un tested, from : Htaccess: add/remove trailing slash from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
To Force HTTPS use: (What i use and works with any domains)
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NC,QSA]
As for the value inside the [bracket], it can change depending on you configuration or needs.

Related

RewriteRule with question marks htaccess

i know this been asked a lot
but i still did not succeeded to do it
i have this in my htaccess
RewriteRule ^embed/([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
i want it to be
RewriteRule ^embed/?v=([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/?v=([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
adding ?v=
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/%1([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [NC,L]
RewriteRule ^embed/%1([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [NC,L]
not working
example
mysite.com/embed/?v=abcde
mysite.com/embed/?v=abcde/6x6
currently it is
mysite.com/embed/abcde
mysite.com/embed/abcde/6x6
so now this work
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1 [L]
i have noticed that i have this in the file
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_CGI_AUTHORIZATION:%1]
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^$ /cgi-bin/index_dl.cgi?op=download1&id=%1 [NC,L]
i am still trying to make this to work
RewriteCond %{QUERY_STRING} ^v=(.*)/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=$2&h=$3 [L]
You are not using the rules properly - passing %1 to the rules won't help you, because you're not matching correctly. REQUEST_URI and QUERY_STRING are two different things. RewriteRule is only able to check the REQUEST_URI, and not the QUERY_STRING, as shown in the answer suggested by Panama Jack.
To achieve what you want, you need to use the following instead:
RewriteEngine On
# Check for mysite.com/embed/?v=abcde/6x6
RewriteCond %{QUERY_STRING} ^v=([^&/]+)\/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=%2&h=%3 [L]
# Check for mysite.com/embed/?v=abcde
RewriteCond %{QUERY_STRING} ^v=([^&]+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=%1 [L]
Here, we simply check the query string for each case. If there is a match, only rewrite if we're at /embed/ (trailing slash optional - you may remove the question mark if you wish for the trailing slash to be required).

.htaccess redirect for all files but with exceptions

I know this has been asked thousands times, but I've been googling for three hours without any result. So I'm asking here. I'm creating a website. All the content is in the folder /subfolder. Now I want to redirect all the requests to another domain (let's say domain.com), at the exception of the files that I actually use. It may seem weird but it makes sense in my situation. So what I have at the moment is
RewriteEngine on
RewriteRule ^account$ account.php
RewriteRule ^home$ myhome.php
RewriteRule ^options$ options.php
RewriteRule ^login$ login.php
RewriteRule ^links$ editlinks.php
RewriteRule ^help$ howto.php
RewriteRule ^$ index.php
RewriteRule ^forgot$ forgot.php
RewriteRule ^r$ redirect.php
RewriteRule ^r/(.*)$ redirect.php?id=$1
RewriteCond %{REQUEST_URI} !(login.php|login)
RewriteRule (.*) external.php?parameter=$1 [L]
How can I do this ? the above code always redirects me to domain.com.
Thank you in advance
Make sure to place redirect just below RewriteEngine On and use THE_REQUEST instead of REQUEST_URI like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{THE_REQUEST} !/media [NC]
RewriteCond %{THE_REQUEST} !/(login|signup)(\.php)?[?\s] [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]
RewriteRule ^home$ myhome.php [L]
RewriteRule ^links$ editlinks.php [L]
RewriteRule ^help$ howto.php [L]
RewriteRule ^(account|login|options|forgot)/?$ $1.php [L,NC]
RewriteRule ^r$ redirect.php [L]
RewriteRule ^r/(.+)$ redirect.php?id=$1 [L,QSA]

Stuck on RewriteRule in htaccess

I am stuck in redirecting my urls. I think i am almost there but can't oversee what i am missing. When i test the redirect is goes to the correct url, but sticks the old url behinde the new url.
This is where i got stuck:
RewriteCond %{QUERY_STRING} lang=uk&page=page
RewriteRule ^(.*)$ domain/page/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=fr&page=page
RewriteRule ^(.*)$ domain/page/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=about
RewriteRule ^(.*)$ domain/about/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=about
RewriteRule ^(.*)$ domain/about/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=news
RewriteRule ^(.*)$ domain/info/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=fr&page=news
RewriteRule ^(.*)$ domain/info/ [L,R=301]
I am also scared i get stuck with a big htaccess file if i do all redirects individual. Would that slow things down?
Lang=nl, uk, us, fr all go to the same page.
not all page= got the same pagename after the change.
Thanks in advance,
You can have specific redirects first where page name is not same as target followed by a single rule where query parameter page has same value as the target URI. So your code can be shortened to these 2 rules only.
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=news
RewriteRule ^ /info/? [L,R=301]
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=([^&]+)
RewriteRule ^ /%2/? [L,R=301]

htaccess redirects the domain, but not individual pages?

I have a website that has changed domains. I have 4 specific pages that need to be redirected to their new equivalents on the new domain. Other than that, I want the entire domain to redirect to the new one.
Here is my code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# Redirect individual pages.
RewriteRule http://olddomain.net/subdirectory/subpage1/$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]
RewriteRule http://olddomain.net/subdirectory/subpage2/$ https://www.newdomain.com/newsub/newpage2/ [R=301,L]
RewriteRule http://olddomain.net/subdirectory/subpage3/$ https://www.newdomain.com/newsub/newpage3/ [R=301,L]
RewriteRule http://olddomain.net/subdirectory/subpage4/$ https://www.newdomain.com/newsub/newpage4/ [R=301,L]
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
I have also tried writing the RewriteRules like this, but it has the same effect:
RewriteRule ^/subdirectory/subpage1/$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]
This only seems to perform the last line, that is, it always redirects to http://www.newdomain.com/, regardless of what olddomain.net URL was entered. How do I get the individual URLs to redirect?
SOLVED: Here is what I ended up using:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage1/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage2/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage2/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage3/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage3/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/subdirectory/subpage4/$
RewriteRule ^.*$ https://www.newdomain.com/newsub/subpage4/ [R=301,L]
Redirect 301 / http://www.newdomain.com/
the RewriteRule operates on Path not full Uri, the second method you tried works fine, just remove the leading forward slash:
RewriteRule ^subdirectory/subpage1/?$ https://www.newdomain.com/newsub/newpage1/ [R=301,L]
RewriteRule ^subdirectory/subpage2/?$ https://www.newdomain.com/newsub/newpage2/ [R=301,L]
RewriteRule ^subdirectory/subpage3/?$ https://www.newdomain.com/newsub/newpage3/ [R=301,L]
RewriteRule ^subdirectory/subpage4/?$ https://www.newdomain.com/newsub/newpage4/ [R=301,L]
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

Apache 2.2 mod rewrite loop. How to replace [END]

RewriteEngine on
I have following code:
RewriteCond %{REQUEST_URL} ^$
RewriteRule ^files/(.*)$ getfile.php?file=$1 [L]
RewriteRule ^(.+)/index([0-9]*).html$ index.php?path=$1/&page=$2 [QSA]
RewriteRule ^(.+).html$ index.php?path=$1.html [QSA]
RewriteRule ^(.+)/(.+).htm$ index.php?path=$1/&article=$2.htm [QSA]
RewriteRule ^(.+/)$ index.php?path=$1 [QSA]
RewriteRule ^(.+)$ index.php?path=$1 [QSA]
This code translate urls to parameter path and article according to situation. All good and bad url request is routed to index.php except for files within folder files/.
I have problem with last line of code. It is primarily for all bad urls (all other is processed earlier). With this rule it start to loop over and over again, because all of url match this rule. This rule have to match once or never, but without [END] I cannot contrive. I have older version of apache (2.2) so I cannot use this direction.
Solution for this I have tried is something like this:
RewriteCond %{QUERY_STRING} !^.*path=([^&]*).*$
RewriteCond %{REQUEST_URI} !^/((files|design)/.+)$
RewriteRule ^(.+)$ index.php?path=$1 [QSA]
but cannot succeed.
Try adding a L flag to the end of each rule and add a redirect environment check in the beginning to pass through any previously internally redirected URI:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^files/(.*)$ getfile.php?file=$1 [L]
RewriteRule ^(.+)/index([0-9]*).html$ index.php?path=$1/&page=$2 [L,QSA]
RewriteRule ^(.+).html$ index.php?path=$1.html [L,QSA]
RewriteRule ^(.+)/(.+).htm$ index.php?path=$1/&article=$2.htm [L,QSA]
RewriteRule ^(.+/)$ index.php?path=$1 [L,QSA]
RewriteRule ^(.+)$ index.php?path=$1 [L,QSA]