How to redirect invalid URL in .htaccess? - apache

/(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.

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]

Rewrite URL if specific parameter exists

I have a url like this: website.co.uk/search.php?a=category&b=test&c=test
I want to rewrite search.php to /search/ and the variable 'a' only if it exists.
So that the url would redirect to: website.co.uk/search/category/?b=test&c=test
If the url was: website.co.uk/search.php?b=test&c=test
Then this should redirect to website.co.uk/search/?b=test&c=test
Currently I am using:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*?)/?$ search.php?a=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?a=([^\&\ ]+)
RewriteRule ^/?search\.php$ /search/%1? [L,R=301]
If I enter the url website.co.uk/search.php?a=category&b=test&c=test
It redirects to website.co.uk/search/category
How do I add the remaining parameters to the end of this URL?
So that it looks like website.co.uk/search/category/?b=test&c=test
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /search\.php\?a=([^&\s]*)\sHTTP [NC]
RewriteRule ^ /search/%1/? [L,NE,R=301]
RewriteCond %{THE_REQUEST} /search\.php(?:\?a=([^&\s]*)&(\S+))?\sHTTP [NC]
RewriteRule ^ /search/%1/?%2 [L,NE,R=301]
RewriteCond %{THE_REQUEST} /search\.php(?:\?(\S+))?\sHTTP [NC]
RewriteRule ^ /search/?%1 [L,NE,R=301]
RewriteRule ^search/?$ search.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([\w-]+)/?$ search.php?a=$1 [L,NC,QSA]

htaccess to check if query string exist otherwise use previous rule

I am trying to convert this url
http://127.0.0.1/kurumsal/index.php?sayfa=referans&page=2
to this
http://127.0.0.1/kurumsal/index/sayfa/referans/page/2/
but not every page has pagination which refers to page=2 query string. i want to just use first part of query string if 'page' query string do not exist.
So it would be http://127.0.0.1/kurumsal/index/sayfa/referans/ without pagination query string.
followed some tutorials but no luck, here is my htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)page=($|&)
RewriteRule ^sayfa/([^/]*)/([^/]*)/$ /kurumsal/index.php?sayfa=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sayfa/([^/]*)/$ /kurumsal/index.php?sayfa=$1 [L]
Try using this rule:
RewriteEngine On
RewriteRule ^kurumsal/index/sayfa/([^/]*)/page/([^/]*)$ /kurumsal/index.php?sayfa=$1&page=$2 [L]
You can use this rule in root/.htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /kurumsal/index\.php\?sayfa=([^\&]+)(?:&page=)?(.*?)\sHTTP [NC]
RewriteRule ^ /kurumsal/index/sayfa/%1/%2? [L,R]
RewriteRule ^(?:kurumsal/)?index/sayfa/([^/]+)/?(.*?)/?$ /kurumsal/index.php?sayfa=$1&page=$2 [L]
Try this
RewriteEngine On
RewriteBase /kurumsal/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index/sayfa/([^/]*)/page/([^/]*)/$ /kurumsal/index.php?sayfa=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index/sayfa/([^/]*)/$ /kurumsal/index.php?sayfa=$1 [L]

Rewrite GET Query String using .htaccess

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]

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]