Rewrite Rule gives 500 Error - mod-rewrite

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^files/(.*) files/index.php
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
I am trying to rewrite everything in the files directory to index.php which is also in the files directory. The page displays but always gives a 500 error , can anyone help me please ?

Do this in the same order:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/files [NC]
RewriteRule ^(files)/.* $1/index.php [L]

Related

Remove/Redirect public/index.php from url to prevent duplicate urls

Hello i am facing issue to remove the public/index.php from url. Remove/Redirect index.php from url to prevent duplicate urls
This link really helps me to remove index.php form url but i am not able to remove public/index.php from url. Here is my below htacces code
RewriteEngine On
#REmove index.php from url starts
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
#Remove index.php from url ends
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
But when i add this below code to remove public/index.php in htaccess its not working :-
RewriteCond %{THE_REQUEST} /public/index\.php [NC]
RewriteRule ^(.*?)public/index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /public/index\.php [NC]
RewriteRule ^(.*?)public/index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
Example urls that should be redirected:
mydomain.com/public/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/public/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com?anything (anything can contain any characters)
Pleae help me how to resolve this issue.
You may use this rule inside public/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/index\.php(?:[/?](\S+))?\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]
RewriteCond %{THE_REQUEST} /public/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# other rules below this line
Additionally use this code in site root .htaccess:
RewriteEngine On
# remove /index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteRule ^ public%{REQUEST_URI} [L]

Having difficulty OHS rewrite rule for multiple domains

I'm having a bit of difficulty with rewriting on Oracle HTTP Server for multiple domains that point to same IP address and port
Following is working
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
However when I try https://sub-doamin-2/analytic it redirects to the https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL
Tried RewriteCond ${HTTP_HOST} method with no luck. It just redirect to / (root)
RewriteEngine On
RewriteCond ${HTTP_HOST} sub-doamin-1$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond ${HTTP_HOST} sub-doamin-2$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-2/analytics
Can you please assists resolving this issue?
It should be %{HTTP_HOST} instead of ${HTTP_HOST}
So the rules should be:
RewriteCond %{HTTP_HOST} sub1.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub1.test.com/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond %{HTTP_HOST} sub2.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub2.test.com/analytics [L]
You can check the rules here: https://htaccess.madewithlove.be?share=6632e45c-a7bb-5099-ab0b-468ba1066277
for the urls https://sub1.test.com and https://sub2.test.com
If you write your original rules in that website you will get This test string is not supported: ${HTTP_HOST} so this can also help you next time.

Laravel .htaccess with forced HTTPS

I use this .htaccess file in root directory of my Laravel website on hosting, so the visitors dont see /public/ in URL address:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^my-domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.my-domain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
But so far I couldnt find a simple solution to add forced HTTPS redirection. Any suggestions please?
Force domain to use https using .htaccess
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
modify your .htaccess with this code:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
</IfModule>
This will help you
You should try below in .htaccess
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.my-domain.com/$1 [R=301,L]

mod-rewrite weird behaviour on rewriterule

My .htaccess file looks like this:
Options +FollowSymlinks
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^product/([0-9]+)/*. /tags.php?id=$1 [NC,L]
RewriteRule ^page/([a-zA-Z0-9_-]+)$ /site.php?page=$1 [NC,L]
I wanted the last rule to be:
RewriteRule ^site/([a-zA-Z0-9_-]+)$ /site.php?page=$1 [NC,L]
If I change the last rule, it does not work at all. I get a 404 not found.
I have no idea why it doesn't work, is the word 'site' a registered word or...?

How to write the mod_rewrite rules for these requirements?

I need to make rules for mod_rewrite:
from
http://site.kiev.ua/index.php
http://www.site.kiev.ua/
http://www.site.kiev.ua/index.php
to
http://site.kiev.ua/
from
http://site.kiev.ua/catalog/products/941
to
http://site.kiev.ua/catalog/products/941/
from
http://site.kiev.ua/catalog/products/941/index.php
to
http://site.kiev.ua/catalog/products/941/
941 - it may be any category
RewriteEngine On
RewriteBase /
# www to none www
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.(html?|php)$ ./$1 [R=301,L]
# Force trailing slashes.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# /file.php to /file/
RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^([^/]+)/?$ ./$1.php [QSA,L]
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.site\.kiev\.ua$ [NC]
RewriteRule ^(.*)$ http://site.kiev.ua/$1 [L,R=301]

Resources