Mod Rewrite stop at rule? - apache

I have the following mod rewrite in my .htaccess and when I go to /commPortal.php it still ends up routing me to index2.php.
RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index2.php [QSA,L]
RewriteCond %{REQUEST_URI} \.php [NC]
RewriteRule ^ index2.php [QSA,L]
This seems to be due to RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [L] then getting picked up by:
RewriteCond %{REQUEST_URI} \.php [NC]
RewriteRule ^ index2.php [QSA,L]
Is there any way to get the RewriteCond %{REQUEST_URI} \.php [NC] to see the commPortal.php rather than /commportal or even have it ignored if there was already a matching rewrite rule?

Change your last rule to this:
RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [L,QSA,NC]
RewriteRule ^index2\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index2.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ index2.php [L,NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$ condition will be true only if there has been no internal rewrite before this rule thus executing this rule only no other rule has been fired before this.
Another option to use THE_REQUEST variable that contains original request not the rewritten one:
RewriteCond %{THE_REQUEST} !\s/+commportal/ [NC]
RewriteRule \.php$ index2.php [L,NC]

Could you please try following, based on your shown samples only. Please make sure to clear your browser cache before testing your URLs.
RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index2.php [QSA,L]
RewriteCond %{REQUEST_URI} !commportal\.php/?$ [NC]
RewriteCond %{REQUEST_URI} \.php [NC]
RewriteRule ^ index2.php [QSA,L]

Related

dynamic url change issue

I want to change my URL from https://website.com/free?s=new-check to https://website.com/free/new-check/
My Current htaccess code :
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{THE_REQUEST} /free\?s=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ /free?s=$1 [NC,L]
It works and directed to https://website.com/new-check but what I want is https://website.com/free/new-check/
previously I removed the .php extension from the URL.
I tried a lot of solutions but nothing works. please help me
With your shown samples, please try following htaccess Rules. Make sure to keep these Rules at top of your htaccess file if you have other Rules also in your file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##https apply rules here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##External redirect Rule here.
RewriteCond %{THE_REQUEST} \s/(free)\?s=([^\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301]
##Internal rewrite rule here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ free.php?s=$1 [NC,QSA,L]

url Rewrite two urls in a folder

I want to rewrite Two URLs in a folder
first is
www.example.com/mybooks/list.php?id=novel-15 to www.example.com/mybooks/novel-15 for this i have the following code in mybooks/.htaccess file [.htaccess is in mybooks folder]
RewriteEngine on
RewriteBase /mybooks/
RewriteCond %{THE_REQUEST} /list\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ list.php?id=$1 [L,QSA]
Its working fine...
Now I want to rewrite
mybooks/search.php?search_word=mystery&search_option=all&page=4 to mybooks/mystery/all/4
i want to do this with out distrub the first rewrite option how can i do this
You can use:
RewriteEngine on
RewriteBase /mybooks/
RewriteCond %{THE_REQUEST} /search\.php\?search_word=([^\s&]+)&search_option=([^\s&]+)&page=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3? [R=302,L]
RewriteCond %{THE_REQUEST} /list\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/(\w+)/(\d+)/?$ search.php?search_word=$1&search_option=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ list.php?id=$1 [L,QSA]

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

URL RewriteRule in .htaccess for index.php query parameters

Example URL's:-
www.domain.com/index.php?bob
www.domain.com/index.php?jane
www.domain.com/index.php?fred
Need rewriting like:-
www.domain.com/bob
www.domain.com/jane
www.domain.com/fred
Have tried with many variations now but the closest I can get to is:-
www.domain.com/?bob
www.domain.com/?jane
www.domain.com/?fred
The below in .htaccess achieves this...
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Please could someone point out what I need to modify to bin the ? (question mark) in the URL?
Edit
Just noticed that since applying the answer given by anubhava below that robots.txt for example doesn't resolve to the .txt file but just displays the homepage.
.htaccess below:-
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#pos1
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA] <--
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
#pos2
If I add the <-- line in pos1, the robots.txt URL returns a 404 page not found error.
If I add the <-- line in pos2, the robots.txt URL just displays the homepage.
Edit2
In the meantime, I have excluded robots.txt from rewrites by adding the following under Rewrite Base /:-
RewriteRule ^robots.txt - [L]
You can use this code:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+index\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA]
You can also use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

mod_rewrite rule keeps in loop

I am doing a quite simple rule for mod_rewrite yet I get a loop
Here are the rules:
first if I have a request like
index.php/SOMETHING.(txt,jpg,php)
I need to first check if /SOMETHING.(txt,jpg,png) exists and display it
if the file is not an index.php/SOMETING and is a real path that exists display it
if not...pass it on to the index.php/SOMETHING.(txt,jpg,php) and show index.php
It all works but the last rule if I have a unexistent txt,jpg,php
example : http://domain.com/index.php/robots1.txt
File does not exist: /Applications/XAMPP/xamppfiles/htdocs/testredir/robots1.txt
works for any other extension...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^index.php/.+$
RewriteRule ^index.php/(.+)\.jpg$ $1.jpg [NC,L]
RewriteRule ^index.php/(.+)\.txt$ $1.txt [NC,L]
RewriteRule ^index.php/(.+)\.php$ $1.php [NC,L]
#here I am missing a rule but if i put the following it will loop
#RewriteCond %{REQUEST_URI} -f
#RewriteRule .* index.php/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^index.php/.+$
RewriteRule .* index.php/$0 [PT]
The %{REQUEST_URI} variable always starts with a /, and you're matching against it using ^index.php/.+$, so that condition will always be false.
It looks like you want something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /index\.php/(.+)\.(jpg|txt|php)
RewriteRule ^ /%1.%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L,PT]