Combine htaccess rules - apache

I have the following .htaccess in parent directory :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Then i have another .htaccess in a sub directory :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /mobile/new_mobile/index.html [NC,L]
Each of them separately does its job and works fine, but i want to use them both combined but cant seem to figure out how, i tried both :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /mobile/new_mobile/index.html [NC,L]
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
and
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /mobile/new_mobile/index.html [NC,L]

You can use this combined .htaccess in site root .htaccess (parent directory):
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login\.php [NC]
RewriteRule ^ https://%{HTTP_HOST}/%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^sub/(.*) /mobile/new_mobile/index.html [NC,L]

Related

showing error on htaccess address rewrite

i am want to convert url:
/tld/?=com to /tld/com/
my full htaccess code is as below:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(whois|error)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##### Rewite #####
##RewriteRule ^([^/]*)\$ /tld.php?tld=$1 [L]
RewriteRule ^([^/]+)\$ /tld.php?tld=$1 [L]
but its show 500 error so please help
Have it this way:
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(whois|error)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# ignore all rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^tld/([\w-]+)/?$ tld.php?tld=$1 [L,QSA]

How to redirect and in the same time allow any files in directory .htaccess?

currently, I've used this code in my htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
Options -Indexes
This will hide my directory and let me view my file, but how to put together redirect (code below) into above code?
RewriteCond %{HTTP_HOST} !^www\.nextdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.nextdomain.com/$1 [L,R=301]
Okay here the answers after get guide from #Nic3500
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} !^www\.nextdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.nextdomain.com/$1 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

URL Friendly using htaccess

Having the below urls, but only the first one works with my htaccess code. Please help to fix this for both url at time. Thank you.
URL 1: http://domain.com/index.php?id=Apple
Output : http://domain.com/Apple (This is ok)
URL 2: http://domain.com/index.php?id=Apple-Board&jid=Chief-Accountant
Output : http://domain.com/Apple-Board (This is not ok)
Expected Output : http://domain.com/Apple-Board/Chief-Accountant
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
RewriteRule /id/(.*)/jobid/(.*)/ index.php?id=$1&jid=$2
You can use:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^\s&]+)&jid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ index.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?id=$1&jid=$2 [L,QSA]

Using mod rewrite to rewrite a particular url string in codeigniter

I'm not sure if mod rewrite should be used here:
I'd like to rewrite one url:
www.baseurl.com/url-with-hyphens
to:
www.baseurl.com/controller/method
This is a codeigniter application, here are my current rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I have tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^.registration/available/2 /u-s-course-schedules/
Thanks

How to exclude a subdirectory from an Apache URL rewrite condition

I have the following rewrite conditions applied to the root directory of my Apache2 webserver (in apache.conf)
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
I have a subdirectory, www.domain.com/subdir that I don't want these rewrites to apply to. How can I exclude subdir from these options?
Actually you just need to move up your last rule of skipping files/directories. Try this code:
Options +FollowSymlinks
RewriteEngine On
# if request is not for a file/directory
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
# then skip from rewrites
RewriteRule ^ - [L]
# add www to hostname
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# forward all the requests to `/index.php
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Right after this line
RewriteEngine On
Add:
RewriteRule ^/subdir/ - [L]
or
RewriteCond %{THE_REQUEST} \ /+subdir/
RewriteRule ^ - [L]