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

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

Related

Adding a Rewrite rule to my PHP code

I could not find a satisfying answer nowhere, and i know very basics about Rewrite Rules, but just can't find a way to achieve that. I would like to clip a certain portion of my address:
/entry.php?id=howdy-world
so, I would like it to look like this:
/entry/howdy-world/
I know how to point to a default file, or Rewrite get values to become numbers after the trailing slash, but the portion to be trimmed is kind of in the middle. How to deal with that?
Below is the basic syntax of how to forward from one url to another. The below has a few sections
([^/] +): This matches any character multiple times up to a slash. ex: entry/howdy-world/
$1: This would take what was after entry/ and append it to the required url. ex: entry.php?id=howdy-world
More information on rewriting can be found on the Apache website
RewriteEngine On
RewriteRule ^entry/([^/]+)$ entry.php?id=$1 [L]
# Variations of above rule depending on server setup
# RewriteRule ^entry/(.+)$ /entry.php?id=$1 [L]
# RewriteRule ^entry/(.+)$ http://www.example.com/entry.php?id=$1 [L]

Using .htaccess for a variable folder

I am having a little trouble with using .htaccess for what I need it to do.
This is the URL of the webpage:
http://example.com/folder/$1/topic.php?id=$2
The only two things that change are $1 and $2.
I was using this code, but I cannot seem to get it to work with the variable folder.
RewriteRule ^([a-zA-Z0-9_-]+)$ topic.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ topic.php?id=$1
RewriteRule ^folder/$1/([a-zA-Z0-9_-]+)$ folder/$1/topic.php?id=$2
RewriteRule ^folder/$1/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2
I may have made some mistake with the syntax but I cannot seem to figure it out.
The browser URL will change to look like:
http://example.com/folder/$1/topic/$2
Thank you for your time ^.^
Yay! user-sama.
I think you need to escape $1, just give it some backslash ninja punch:
RewriteRule ^folder/\$1/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2
Or it might be:
RewriteRule ^folder/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2
^__^
Yeah, i need to explain what's going on, so what's between parentheses in the left becomes $ on the right, the number that comes after $ is the present order of parentheses. So:
# 1st 2nd 3rd 1st────┐ 2nd ┌──────3rd
RewriteRule ^(.*?)/(.*?)/(.*)$ file.php?p1=$1&p2=$2&p3=$3
These parentheses blocks are called capture block, it's RegExp.

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

How to convert plus (+) sign to "=+" with htaccess?

I want to convert every url which contains "+" to "=+"
for example that url:
http://www.bedavaemlaksitesi.com/mersinemlakrehberi210/index3.php?+
should be like this:
http://www.bedavaemlaksitesi.com/mersinemlakrehberi210/index3.php?=+
tried that and few other lines but doesn't work so far, i'm guessing it causes a loop or something.
RewriteRule ^([^/\.]+)+([^/\.]+)?$ $1=+$2 [R]
I'm just gonna give you a literal answer for that specific example. Not sure if that will actually help you:
RewriteCond %{QUERY_STRING} ^([+])$
RewriteRule /index3.php$ index3.php?=(%1) [R,L]
You cannot repleace each + in the QS, as you do need a separate condition to match it first.
Also about your original rule:
RewriteRule ^([^/\.]+)+([^/\.]+)?$ $1=+$2 [R]
Escaping the dot in the charclass is redundant, [^/.] suffices. And you need at least a separator between the two groups / to make sense. But you can't match the query_string there, that's handled separately from the current filepath.
See alsos: ServerFault: Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask? -and- HttpdWiki: Manipulating the Query String

redirecting specific page using htaccess

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.

Categories