Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite - convert query string to hyphenated page link

Mod_Rewrite - convert query string to hyphenated page link

         

screenmates

8:12 pm on Jan 15, 2012 (gmt 0)

10+ Year Member



Hey,

Here is my query URL:

http://www.example.com/search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=visual+basic


I want to convert the above URL to make it look like a search-engine-friendly page:

http://www.example.com/blogs/visual-basic

When the user clicks on the converted short link 2, it should go to the long URL 1 (behind the scenes) listed on the top but the browser should still display the short URL # 2.

TIA

g1smd

8:16 pm on Jan 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This question has been asked at least twice in the last 48 hours.

Link to the URL that you want the user to see and use. URLs are defined in links.

Set up a RewriteRule configured as a rewrite (NOT as a redirect) to handle the request and fetch the content from the internal server filepath where the content actually resides.

Set up a RewriteRule configured as a redirect to redirect URL requests with parameters to the friendly URL. This redirect is there for users continuing to request the old URL after you stop linking to it on the site. This RewriteRule needs a preceding RewriteCond looking at THE_REQUEST to ensure that the rule does not create an infinite rewrite-redirect loop.

Be clear that a rewrite is a URL to file path translation and a redirect is a URL to URL translation. Unless you fully understand the differences in coding and action your quest will fail.

[edited by: g1smd at 8:43 pm (utc) on Jan 15, 2012]

screenmates

8:42 pm on Jan 15, 2012 (gmt 0)

10+ Year Member



Hello g1smd,

Yes, I did search for existing replies for an exact answer before posting but no luck. Could you point me to the posted answers that answers this question (converting a query string to a hyphenated page link) or just include a line of mod_rewrite code?

TIA

g1smd

8:46 pm on Jan 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Google: [site:webmasterworld.com rewriterule the_request rewrite redirect query string jdmorgan]
- 550 results.

Yes, this is a question asked many many times. :)

With a limited amount of volunteers, there's no longer time to keep repeating old answers again.

We're happy to help with specific problems with code you have already tried out.

lucy24

9:42 pm on Jan 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Take care of everything else and the hyphen-to-plus conversion will probably fall into place without any further work. But let's see the rest of the code first.

screenmates

10:23 pm on Jan 15, 2012 (gmt 0)

10+ Year Member



Hi,

Thanks for the reply. I am weak in htaccess logic and tried scouring through Google links for a matching answer. Here is what I tried but it does not work:

RewriteRule ^blogs/([a-zA-Z0-9_-]+)\.html$ /search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=$1 [NC,L]


The regular query-string URLs work fine. I just want to convert query-string links like these:

http://www.example.com/search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=visual+basic

TO hyphenated page links like these:

http://www.example.com/blogs/visual-basic

Please advise...

Thanks for your time.

g1smd

10:31 pm on Jan 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a redirect for that. The redirect will use a RewriteRule with the [R=301,L] flags. It will also have a RewriteCond testing for parameters in THE_REQUEST.

However, you should not be linking to those types of URLs from the pages of your site. The pages should link to the friendly URLs.

screenmates

10:38 pm on Jan 15, 2012 (gmt 0)

10+ Year Member



Yes, I'll be linking to friendly URLs and I need to make them work first. But I know virtually nothing about htaccess or how to write a redirect. I just need that 1-2 lines of code.

Thanks!

screenmates

10:51 pm on Jan 15, 2012 (gmt 0)

10+ Year Member



http://www.example.com/blogs/visual-basic.html
(with .html extension at the end will be just fine as well)

screenmates

10:52 pm on Jan 15, 2012 (gmt 0)

10+ Year Member



Oops! Forgot to include this. Here is the content of my htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php

g1smd

11:13 pm on Jan 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code above is for a very basic internal rewrite. You need to add a redirect or set of redirects using more RewriteRules.

But I know virtually nothing about htaccess or how to write a redirect.

Out of the 80 000 threads in this forum, at least 20 000 of them address redirects of one form or another. Many have example code and clear details of how the code works.

screenmates

12:39 am on Jan 16, 2012 (gmt 0)

10+ Year Member



This forum is different. Going through existing posts and coming up with a *optimized* working script is for people who have knowledge in scripting apache. I am experienced in scripting languages but not in apache. I scoured through posts and came up with a line of code that did not work other than giving a 404 error. I wanted an expert advice and optimal/functional code on this for which I can even pay if necessary. But this post has grown into a long page of replies and replies with no solution whereas a 2-line code could have resolved it. Don't mean to offend anyone here but I have been on dozens of forums for over 15 years with great response and this is the first time I was asked repeatedly to go see the existing posts for a 2-line solution even though I tried my best and came up with some non-functional code. I will probably never be looking at the htaccess file again once this is resolved. I cannot afford to learn the apache scripting just for writing 2-line code other than paying someone for it.

Thanks for all your time.

g1smd

1:03 am on Jan 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem is, you're treating this as two lines of insignificant code. This code is altering the default configuration of your entire webserver, and you need to be up to speed as to what it does and how it works because at the end of the day you will need to maintain that code to keep your site working when you make other changes.

This forum is about helping you to educate yourself to write your own code. It's not a free helpdesk. It contains 80 000 previous threads with just about every question that could be asked, already asked and answered, in some cases hundreds of times over and over again.

So, you can perhaps see why there are few regular contribitors of answers: there are no new answers to post, just old answers to be repeated ad infinitum.

This is an internal rewrite:

RewriteRule ^blogs/([a-zA-Z0-9_-]+)\.html$ /search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=$1 [NC,L]

If you haven't got index.php set by your DirectoryIndex directive it would also fail.

The %5B and %5D are not helping the clarity of reading it, but it looks broken.

You're maybe looking for an external redirect. This operates in the reverse direction to a rewrite.

Let's see your code to discuss...

screenmates

2:03 am on Jan 16, 2012 (gmt 0)

10+ Year Member



Hello g1smd,

Is this the URL to send you a PM? I am confused with as it says "sticky" mail instead of PM:

[webmasterworld.com...]

This is just to control spam on our forum - everytime we post a link to our forum we get spam.

Thanks!

screenmates

2:14 am on Jan 16, 2012 (gmt 0)

10+ Year Member



The URL is encoded and unencoded version uses square brackets indicating comparison operators:

RewriteRule ^blogs/([a-zA-Z0-9_-]+)\.html$ /search-results/?action=search&type[equal]=Blog&keywords[any_words]=$1 [NC,L]

lucy24

3:47 am on Jan 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As written:

User requests nice friendly-looking URL

www.example.com/blogs/blahblah.html

and that's what their browser's address bar will say. But what they see on the screen--

well, it depends on this, which is an entirely unrelated piece of code:
If you haven't got index.php set by your DirectoryIndex directive it would also fail.

When people request

www.example.com/directory/

do they in fact get taken to

www.example.com/directory/index.php

? If you are on shared hosting, they will probably have a short list of default index-file names to try. If your index files are called anything else, you have to deal with it yourself in htaccess using mod_dir. Assuming your configuration allows you to do so. But since you are dealing with a rewrite, not a redirect, there's no reason not to write it out in full. Not / but /index.php

Conversely, since you will be rewriting anyway, what's the .html for in the URL? Personally I like them; when I see an extensionless URL my first impulse is to tell it to go home and put some clothes on. But they do seem to be in fashion currently. So if you're using an extension, understand that it isn't in any way necessary. The "blahblah.html" is not an actual page, and therefore it doesn't need an actual extension. You could just as well say "blahblah.blog" and take it from there. (Bad Idea. But you could do it.)

Meanwhile, your user is waiting patiently for the RewriteRule to kick in. They got as far as typing-- or clicking--

www.example.com/blogs/blahblah.html
or if you prefer
www.example.com/blogs/blahblah

Behind the scenes, your server prepares the page

www.example.com/search-results/index.php

and feeds it the query string

?action=search&type[equal]=Blog&keywords[any_words]=blahblah

This is fine if blahblah corresponds to a pre-existing blog. But you also have to code for blogs that don't exist, including the ones that come from malformed requests like

www.example.com/blogs/.html

Second part of answer
Last time I did this, the questioner stomped off in a huff and was never seen again. But, what the hey. One or more of the following applies to you:
#1 You used to have children
#2 You currently have children
#3 You will have children
#4 You used to be a child

Throw your mind back-- or forward, or sideways-- and consider how long it takes a child to clean its room under parental supervision, compared to how long that same parent could do it on their own. But the time-consuming way leads to a child who knows how to clean its room-- and will grow up to be an adult who knows how to clean up after himself.

That was an analogy ;)

screenmates

6:29 am on Jan 16, 2012 (gmt 0)

10+ Year Member



I forgot to mention - the blogs directory does not exist. It is virtual. The web root has index.php and yes, it is set as the DirectoryIndex.

Thanks!