URL forwarding with two arguments - apache

This is my current .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?u=$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
It turns www.example.com into example.com AND interprets example.com/username AS example.com/index.php?u=username
Now I want to pass a second argument like example.com/index.php?u=username&e=email and still keep the format example.com/arg1&arg2. How do I do that in .htaccess?

First, you need to move the redirect before your routing rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Then, you want to check the 2 argument route:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA]
(this assumes a URL that looks like: http://example.com/username/emailaddress, but if you really want the & to separate them, use this rule instead:
RewriteRule ^([0-9a-zA-Z-]+)&([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA]
And this assumes a URL that looks like: http://example.com/username&emailaddress
Now your original rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ /index.php?u=$1 [L,QSA]

Related

Infinite redirect after htaccess

RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus/? [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
I changed it for
xyz.com/worksheet/rebus/?random=testing11
to be forwarded for
xyz.com/worksheet/rebus/
buts its endup in infinite redirect.
With your shown samples, please try following htaccess Rules file. Please make sure you keep your htaccess Rules file along with worksheet folder NOT inside worksheet folder.
Clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus/? [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?tableName=$1 [L,QSA]

.htaccess rewrite to add country code (if missing)

I am using a .htaccess file in the subdirectory /cms and using this subdirectory as RewriteBase. The redirections go to 'backend.php' and send the variables I want. It is a dual language site (nl|en), dutch and english. Everything works fine, as long as the %{REQUEST_URI} starts with (nl|en).
But I need a fallback to the default dutch language when nl|en is omitted, but can I add this to the following .htacces file. I have been trying and searching but cannot find the right syntax to make this happen:
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !(^(nl|en)/)
#RewriteRule (.*) This is where the solution should be used ?
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule ^(nl|en)/([^/]+)/$ backend.php?page=$2&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/$ backend.php?page=$2&id=$3&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/(.+)/$ backend.php?page=$2&id=$3&task=$4&language=$1 [QSA,NC,L]
One way to handle this is a rewrite with the dutch language prepended. This will silently change direction to the proper page.
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [L]
If you want the client to notice and change the URL, you must do a R|redirect instead
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [R,L]
Your default rule can be this one:
# This is where the solution should be used ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(nl|en)/ [NC]
RewriteRule ^([^/]+)/?$ backend.php?page=$1 [QSA,L]

Dynamic htaccess code for multiple php parameters to friendly urls

I have searched and searched for an answer to this, but none of the posts I've found on stackoverflow work for me - nor do the online htaccess generators.
I have made slight adjustments to Anubhava's excellent answer on htaccess redirect for dynamic urls not working to suit different page names as follows:
RewriteEngine On
# for external redirection from `/hp.php?su=sitename` to `/sitename`
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# for internal redirection from `/sitename` to `/hp.php?su=sitename`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
However, in addition to sending users to domain.com/sitename (which the modified code above does) I also want to change the following urls.
domain.com/newpage.php?su=sitename&PgID=1234&pu=pagename
to become
domain.com/sitename/1234/pagename.html
similary
domain.com/diary.php?su=sitename
to become
domain.com/sitename/diary.html
This last one would be replicated for similar dynamic pages, such as
future.php?su=sitename >> domain.com/sitename/future.html
photos.php?su=sitename >> domain.com/sitename/photos.html
etc
Some time in the future, I would also like to divert http:// to https:// - would this rule go before all the others?
Hope somebody can help
UPDATE:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&PgID=$2&pu=$3 [L,QSA]
It isn't all that difficult to derive from the previous rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&pgID=$2&pu=$3 [L,QSA]
As to your second question about http to https, yes; that particular redirection should occur before all others.

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]

Redirect www to non-www is loosing my other RewriteRule

I have this code in my, .htaccess file.
My url is fine foodzite.dk/vis/1234567
When i enter www.foodzite.dk/vis/1234567 i want it to redirect to foodzite.dk/vis/1234567
but i redirects to foodzite.dk/shop.php?url=1234567
How is it i cant combine the to Rewrites?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
You need to have your redirect rule before your routing rule. Otherwise the URI gets routed to your php file, then the redirect rule gets applied and the URI is exposed:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
Additionally, your conditions only get applied to the immediately following rewrite rule, so the 2nd and 3rd don't have the same conditions. (Unless you wanted it to be that way).
Try something like:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^foodzite.dk [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]