I use the following rule to redirect pretty urls from http://hostname.co.za/geoip/123.12.123.34 to http://hostname.co.za/geoip/index.py?ip=123.12.123.34
.htaccess in /geoip
RewriteEngine on
RewriteRule ^(.*\..*\..*\..*)$ /geoip/index.py?ip=$1
This works fine to match only ip's, but when I try this, it gives a 500 server error:
RewriteEngine on
RewriteRule ^(.*\..*)$ /geoip/index.py?ip=$1
I want to match not only ip's, but hostnames with at least one fullstop as well.
I have no idea why this shouldn't work...
The problem with the second pattern is that it also matches index.py and thus yields an infinite recursion. You can exclude that by using a RewriteCond:
RewriteCond $1 !=index.py
RewriteRule ^(.*\..*)$ /geoip/index.py?ip=$1
Furthermore, you should use a more specific pattern than .* like [^/.]+, so:
RewriteCond $1 !=index.py
RewriteRule ^([^/.]+\.[^/.]+)$ /geoip/index.py?ip=$1
RewriteRule ^([^/.]+\.[^/.]+\.[^/.]+\.[^/.]+)$ /geoip/index.py?ip=$1
Related
I want to feed several URLs into a single php file that will handle the contents of the page, the URLs are like
domain.com/fashion-registration
domain.com/singing-registration
I want to capture URLs ending with -registration and feed fashion or singing into the page but it doesn't seem to be working. This is what I tried
RewriteRule ^(.*)$-registration category.php?link=$1 [NC,L,QSA]
Could you please try following.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(.*)-registration/?$ [NC]
RewriteRule ^(.*)$ /category.php?link=%1 [NE,NC,L]
OR you could try following too, one without RewriteCond.
RewriteEngine ON
RewriteRule ^(.*)-registration/?$ /category.php?link=$1 [NE,NC,L]
Problem in OP's attempt: Since you have used ^(.*)$ and after that you are using -registration in your regex that's why your regex is NEVER going to match -registration
I have a rule in my htaccess file to remove any extra trailing slashes from a url, this works on sub-directories with any more than 1 trailing slash. However it doesn't work on the root; which i need it to do.
For example.
http://www.example.com/test//// Redirects to http://www.example.com/test/
http://www.example.com/// Needs to redirect to http://www.example.com
Any ideas on what i need to add?. Cheers.
RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . %1/ [R=301,L]
For removing multiple slashes anywhere in REQUEST_URI this rule works best:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=301,L,NE]
It takes advantage of the fact that mod_rewrite engine itself converts all multiple forward slashes to a single slash in the RewriteRule pattern. We use RewriteCond %{THE_REQUEST} to make sure original REQUEST_URI contains multiple slashes.
Here [^?]*// matches 2 // before matching query string since [^?] matches anything except ?. This will allow // in query string.
Try with:
RewriteCond %{REQUEST_URI} ^(.*?)//+$
RewriteRule ^ %1/ [R=301,L]
You htaccess works great as you can test on below link
https://htaccess.madewithlove.be/
So you need to make sure you test either with a Chrome Incognito window or using like below
curl -v http://example.com////
I usually prefer curl as I know it will give a fresh response from the server always
You just need two rule to match two different pattern
RewriteCond %{REQUEST_URI} ^(?:/){2,}$
RewriteRule . / [R=301,L]
RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . %1/ [R=301,L]
RewriteEngine on
RewriteRule ^(.*)$ http://www.NEWfoobar.com/$1 [R=301,L]
I have this line of code from https://stackoverflow.com/a/7578810, inside .htaccess and it currently works by migrating www.OLDfoobar.com to www.NEWfoobar.com and all its sub-directory structure just fine (intended behavior, all good)
now, how do I add additional rule that captures www.OLDfoobar.com to redirect to one specific page in www.NEWfoobar.com? say for example `www.NEWfoobar.com/welcomeToNewDomain
The [L] flag is what gets me. and btw, I'm not so sure about what the $1 is for. That can't be a regex flag for the RewriteRule pattern, yes?
ACCEPTED ANSWER
RewriteEngine on
RewriteRule ^/?$ http://www.NEWfoobar.com/some/sub/directory [R=301,L]
RewriteRule ^(.*)$ http://www.NEWfoobar.com/$1 [R=301,L]
You need
RewriteEngine on
RewriteRule ^(.*)$ http://www.NEWfoobar.com/welcomeToNewDomain [R=301,L]
The $1 meant use the first capture group which is the value between the brackets on the left.
The L in the options means stop processing the rewrite rules (ie this is the Last Rule) if the match applies.
The ^(.*)$ means:
`^` match beginning of string
`(.*)` match zero or more characters and store in capture group 1
`$` match end of string
If I've miss understood an you only want to redirect the root requests while keeping the old capture you need to add a new rule before the existing one that reads
RewriteRule ^/?$ https://www.NEWfoobar.com/welcomeToNewDomain [R=301,L]
This will match either an empty path or just /
I have a programmatically generated set of rules for a site like this:
RewriteEngine on
RewriteRule ^abc/?(.*)$ /$1?organisation=abc [QSA,L]
RewriteRule ^fghij/?(.*)$ /$1?organisation=fghij [QSA,L]
RewriteRule ^fghijklmn/?(.*)$ /$1?organisation=fghijklmn [QSA,L]
So that our client can set up multiple minisites on their domain for various clients of their own.
Because the url could end in / or /blah.php I've created the rules as above but this means the RewriteEngine would stop after ^fghij/?(.)$ and never find ^fghijklmn/?(.)$
How could I rewrite my rules so every organisation is properly matched?
Many thanks in advance
Your regex seems to be a problem since ^fghij/?(.*)$ would also match fghijklmn.
Try this code:
RewriteEngine on
RewriteRule ^abc(/.*)?$ $1?organisation=abc [QSA,L]
RewriteRule ^fghij(/.*)?$ $1?organisation=fghij [QSA,L]
RewriteRule ^fghijklmn(/.*)?$ $1?organisation=fghijklmn [QSA,L]
I think that the /? in ^abc/?(.*)$ is saying optionally match / then match anything else after it so you likely want something like
RewriteRule ^abc//?(.*)$ /$1?organisation=abc [QSA,L]
That will enforce a / at the end since you say the URL will always end in / or /blah.php
Also, I am unsure why you don't just try to match the organisation with a non-greedy matcher on the word before the first slash and then append it to the query string instead of creating it manually.
What I'm trying to accomplish with htaccess mod-rewrite:
Redirect all sub-domains to new domain name w rewrite rule.
e.g.
test1.olddomain.com ===> test1.newdomain.com
test2.olddomain.com ===> test2.newdomain.com
test3.olddomain.com ===> test3.newdomain.com
This is what I have so far which of course is wrong:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/$1 [R=301,L]
RewriteRule [a-zA-Z]+\.olddomain.com$ http://$1.newdomain.com/ [R=301,L]
Since I'm not a Regular Expression junkie just yet, I need your help... Thanks for any help you can give here. I know also we can compile these first two conditions into one.
Note: The reason I don't redirect all domain using DNS is that a lot of directories need special rewrite rules in order to maintain positions on SEO.
In .htaccess files, the "URL" that RewriteRules match has been stripped of the domain name and any directories that led to the current directory. (Using mod_rewrite in .htaccess files is a huge pain; if you have access to the server conf do it there instead!!)
So, assuming that your .htaccess is in your DocumentRoot, try something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://%1newdomain.com/$1 [R=301,L]
The %1 is supposed to match the first group in the RewriteCond and the $1 is supposed to match the URL part.
RewriteRule ^(.+)\.olddomain\.com$ http://$1.newdomain.com/ [R=301,L]
You need to specify the ^ at the beginning to ask the regex engine to match a line beginning there. Next, you match anything before ".olddomain.com" and assign that to the first matched pattern (which will later be accessible in $1). You need to surround with parentheses (.+) in order for the match to be assigned to $1.