redirecting specific page using htaccess - php

I want to redirect like http://example.com/sites-related-to-australia-0.html.
In this "australia" and "0" are parameters. How can i do this using htaccess? i'm using php.
Any help greatly appreciated, Thanks!

It would likely be something like this:
RewriteRule ^sites-related-to-(\w+)-(\d+)\.html$ /somewhere_else.php?place=$1&pos=$2 [L]
/sites-related-to- will be the first part of the URL
The next piece (a block of one or more word characters signified by (\w+) (you can also replace this with a more specific (australia|united kingdom|france))) is captured for later as $1
The piece after the next hyphen will be captured as a digit (\d) and it will be stored as $2
Load the page somewhere_else.php with get variables place=$1 and pos = $2 (both defined earlier).
[L] means that this is the last redirect rule which effects this particular pattern.

RewriteRule ^sites-related-to-(\w+)-(\d+).html/$ sites-related-to.html?country=$1&val=$2 [L]

RedirectMatch sites-related-to-([^-]+)-(\d+)\.html$ http://www.anotherserver.com/something.php?country=$1&id=$2
Untested, see RedirectMatch.

Related

To Explode or RewriteRule? Utilizing Variables From Hyphenated URL's

I'm hoping I can make this make sense.
I had URLs' that looked like this
http://www.website.com/state/AZ/Phoenix
And now I've written them to this
http://www.website.com/AZ
using this rewrite code to parse (borrowed)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ /x.php?state=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /x.php?state=$v1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /x.php?state=$1&city=$2 [L]
This works great for parsing the "AZ" portion of the url and using it as a variable. Awesome. However, I wanted to take this to the next step and start using the city, and even crazier? not in the same order.
DESIRED URL FORMAT: http://www.website.com/phoenix-arizona-other-words
NOTE: I understand this doesn't say "AZ", it's fine, I'll convert the state to abbreviation through an array - the more important part is grabbing the first two words, separated by a hyphen and assigning them to variables.
For my code to work correctly I'll need to either find a way to explode the "-" in the URL and assign variables this way...
//my terrible attempt at fixing this the HARD way
$variables = explode("-", urldecode(substr($_SERVER['REQUEST_URI'], 1)));
$city = isset($variables[1]) ? $variables[1] : false;
$state = isset($variables[2]) ? $variables[2] : false;
or...
A RewriteRule could possibly save the day and understand what to do with the newly formatted URL and allow x.php? to utilize the correct variables, all while keeping the desired website.com/phoenix-arizona structure.
I think I'm close, basically, I need a Rewriterule to recognize hyphens and assign them to specific parameters, however I've been searching and tinkering around for over 4 hours on this before finally giving in! Any help would be appreciated, and if I'm not thinking about this correctly, it wouldn't surprise me as it's quite clear my regex (RewriteRule) skills are rudimentary at best and the explode function, if it even works, might be total overkill.
This rule should work to translate your PHP code into a rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)-([^/-]+)-(.+)$ /x.php?state=$1&city=$2 [L,QSA]
ok. I'll try to complete this answer tomorrow. But for the first rule, you might try with
RewriteRule ^([A-Z]){2}/? /x.php?state=$1& [L,QSA]
This means: match exactly two uppercase characters from A to Z, followed by an optional trailing slash and whatever comes next
This will turn
http://www.website.com/AZ
into
http://www.website.com/x.php?state=AZ
AND
http://www.website.com/AZ/whatever-url
into
http://www.website.com/x.php?state=AZ&whatever-url
Note that I have appended #anubhava QSA flag to append any query string after the slash to the rewrite rule. I could have captured that part with a wildcard (.*) too. I like his way more than mine.
States are easy because thanks to the USPS they all have kinda standard two character codes.
Edit: now for the second rule
RewriteRule ^([^-]+)-([^/-]+)- /x.php?statelong=$2&city=$1& [L,QSA]
will turn
http://www.domain.com/tucson-arizona-whatever-comes-next
into
http://www.domain.com/x.php?statelong=arizona&city=tucson&whatever-comes-next
Note that I inversed the captured items so I pass the second one to statelong. This way on your PHP you'll know that the state name needs to go through your dictionary array to get its standard USPS 2 character code.
Again, the "whatever comes next" gets appended thanks to the QSA flag. You'll need to capture that part with php by printing out the $_GET superglobal and looking for the orfan key.
Now, what happens when you get
http://www.domain.com/new-york-new-york-these-vagabond-shoes
Of course the rule won't work. Besides, the song was written when there was a New York County, roughly equivalent to today's Manhattan. (Irrelevant trivia).
The next is just an idea. I'm sure you can come with a more creative way
You need a way to tell cities from states from rest of the url. One way to do it is to use underscores to separate composite names, comma to separate city from state and hyphen for the rest.
This rule
RewriteRule ^([\w_]+),([\w_]+)- /x.php?statelong=$2&city=$1& [L,QSA]
will turn
http://www.domain.com/new_york,new_york-this-vagabond-shoes
to
http://www.domain.com/x.php?statelong=new_york&city=new_york&this-vagabond-shoes

mod_rewrite RewriteRule for abc.php?a=1&b=2 to abc/2.html

I am a real newbie to the either mod_rewrite or Regex.
Therefore I just need your help for the following problem.
I got a PHP-Page that looks just like:
stuff.php?id=1&text=2
I know want to to look like
stuff/2.html
Do anyone of you have the RewriteRule line for the htaccess in mind to let it look just like this?
Thanks a lot in advance!
A rewrite rule for this particular page:
RewriteRule ^stuff/2\.html$ stuff.php?id=1&text=2
And if 2 should be dynamic:
RewriteRule ^stuff/([0-9]+)\.html stuff.php?id=1&text=$1
A little explanation:
^ and $ stand for start and end of the string, so we don't match longstuff/2.html.php.
The dot has to be escaped \. because otherwise it has a special meaning in RegEx ("any character")
the parantheses in the second pattern are a "capture group", their content will be available in the rewrite as $n (with n = number of capture group, in this case 1)
[0-9] is a character class, matches one character of the class, in this case a digit
+ means "one or more"
Here's a rule to redirect stuff/2.html to stuff.php?id=1&text=2
RewriteRule ^stuff/([\d]+)\.html$ stuff.php?id=1&text=$1 [L]
Notice [\d]+ will only accept numbers, if you want to allow letters and caret, use the following rule :
RewriteRule ^stuff/([\w-]+)\.html$ stuff.php?id=1&text=$1 [L]

URL Rewrite with htaccess and PHP

I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.

Trouble with URL rewriting in .htaccess

My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1

Passing and URL as parameter in mod-rewrite

I'm trying to pass an URL as a parameter in mod-rewrite. I guess there is a problem in my Regex. This my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule **^go/((http:\/\/)+[A-Za-z0-9\-]+[\.A-Za-z])/?$** feedmini.php?url=$1 [L]
</IfModule>
the URL I want to pass looks like http://www.aaaa.com/aaa/?q=v but when ever I try to reach it on go/http://www.aaaa.com/aaa/?q=v I get an 404 error page. I've also tried with **^go/([A-Za-z0-9\-\/:]+[\.A-Za-z]+)/?$** but then the URL i pass gets like this: http:/www.aaaa.com/aaa/ (observe the singel '/' after 'http:');
Any Ideas?
Thanks in advance
/Ale
Well your first problem (in your first code block) is that your Regex pattern will not match a URL since it will only match a string that begins with http:// then contains nothing but alphanum or dashes, which ends with a single fullstop or letter. Perhaps this is simply a typo and there should be a quantifier in there, but even so it would fail to match a very large percentage or URLs.
This may seem a little strange, but try this...
RewriteRule ^go/http:/(.*)/?$ feedmini.php?url=http://$1 [R=302,L]

Categories