Rewrite GET Query String using .htaccess - apache

I want to Rewrite my GET Query String from this -
http://www.example.com/verify.php?key=547b52f2b5d20d258e62de1e27b55c
to this
http://www.example.com/verify/547b52f2b5d20d258e62de1e27b55c
I am using the following rule but it does not seem to work -
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
RewriteRule ^[A-Za-z-]+/([A-Za-z0-9-]+)/?$ verify.php?key=$1 [NC,L]

Use the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /verify\.php\?key=([^\s&]+) [NC]
RewriteRule ^verify\.php$ /verify/%1? [R=301,L]
RewriteRule ^verify/([^/]+)/?$ /verify.php?key=$1 [NC,L]

Related

Mod Rewrite stop at rule?

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]

Different .htaccess rewrite rules depending on a $_GET param

Is there a way to make different rewrite rules in .htacess if a certain GET parameter was passed?
For example, if query string is:
htttp://domain.com/?debug=1, than .htaccess should look like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
If there is no debug=1 in query string, than:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L]
You can use RewriteCond like this:
RewriteEngine on
# ?debug=1 is present
RewriteCond %{QUERY_STRING} (^|&)debug=1\b [NC]
RewriteRule (.*) app/webroot/$1 [L]
# ?debug=1 is not present
RewriteCond %{QUERY_STRING} !(^|&)debug=1\b [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L]
You want to match against the query string in a RewriteCond:
RewriteCond %{QUERY_STRING} ^debug=1$ [NC]
RewriteRule (.*) app/webroot/$1 [L]
For more information, see the docs or this example.

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]

How to redirect invalid URL in .htaccess?

/(directory/?param=value) cannot change this url
directory/?param=value
to
directory/value
It's not work:
RewriteCond %{REQUEST_URI} ^/directory/.*
RewriteRule ^/directory/\?param=(.*) /directory/$1 [L]
My problem is how to replace ?param= ???
These rules should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(directory)/\?param=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
RewriteRule ^(directory)/([^/]+)/?$ /$1/?param=$2 [L,QSA,NC]
You can't match against the query string (everything after the ?) in a RewriteRule, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?directory/(.+)$ /directory/?param=$1 [L]
That takes: directory/value and rewrites it internally to serve the content at directory/?param=value.

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]