Welcome to WebmasterWorld Guest from 34.204.191.31

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

.htaccess URL rewriting

How to rewrite specific URL

     
8:37 pm on Apr 6, 2014 (gmt 0)

New User

5+ Year Member

joined:Apr 6, 2014
posts: 3
votes: 0


Hello to all,

I am trying to rewrite the following URL:

http://www.example.com/component/ninjarsssyndicator/?feed_id=1&format=raw

to the following new url:

http://www.example.com/index.php?option=com_obrss&task=feed&id=2:kindykids-feed&format=html

Can someone please shed some light and let me know how this can be done? I've tried the following line in .htaccess, which didn't work due to the '?' symbols in the URL, however i'm not sure how to overcome this issue.

Redirect 301 /component/ninjarsssyndicator/?feed_id=1&format=raw /index.php?option=com_obrss&task=feed&id=2:kindykids-feed&format=html

Many thanks.

Chris.

[edited by: phranque at 5:47 am (utc) on Apr 7, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

10:33 pm on Apr 6, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


URLs with query strings can't be done in mod_alias (Redirect by that name). This means that before anything else, you'll have to take all your existing redirects and translate them to mod_rewrite syntax. There are posts laying out the details.

Second issue: You don't mean rewrite. You mean redirect.

A redirect means you're telling the browser "We ain't here no more, go to this new address". The human user doesn't know this; they just see the address bar magically changing.

A rewrite means the browser thinks they're in a certain place, while you're secretly serving content from the ugly messy warehouse out back.

Third issue: Why are you redirecting from one long ugly URL to another one? Seems like this is your chance to prettify the URLs. Then you'll need a supplementary rewrite to serve content from wherever the material really lives. In fact you can do this part without the initial redirect. Why not let your php script handle the changes?

Do a Forums search. There are many, many threads involving the redirect-to-rewrite two-step. It's definitely one of the Top Ten questions over the years.

Finally: See Forums rules about using "example.com". Everywhere else it's a matter of WebmasterWorld principle. Here in Apache it's also a necessity.
5:55 am on Apr 7, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11846
votes: 242


welcome to WebmasterWorld, cpartsenidis!


Redirect 301 /component/ninjarsssyndicator/?feed_id=1&format=raw /index.php?option=com_obrss&task=feed&id=2:kindykids-feed&format=html

as with the Redirect directive, you can only examine the path information in the RewriteRule directive.
therefore if you require examination or capture of the %{QUERY_STRING}(or %{HTTP_HOST}) you must precede the RewriteRule with a RewriteCond directive
9:09 am on Apr 7, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


<begin topic drift>
I had another of my rare glimmerings of insight as it occurred to me why so many people talk about "changing" or "rewriting" URLs. It's because you're thinking in human-user terms: the browser's address bar changes as if by magic even though you didn't type or click anything new.

Most of what the browser does is invisible. One of those invisible things is when it arrives at a server, meets with a redirect, and has to make a whole new request. The ordinary human user doesn't know this, just as you don't know that every picture you see on a web page is there because the browser went out and asked for it individually by name. There's much more to %{THE_REQUEST} than meets the eye.
</topic drift>
4:11 pm on Apr 7, 2014 (gmt 0)

New User

5+ Year Member

joined:Apr 6, 2014
posts: 3
votes: 0


Hi Guys and thanks so much for your replies.

Perhaps the term 'redirect' was not correct, however this is what I've used with all other URLs on the site - Redirect 301.

This is also what I need to do here - The first url:

/component/ninjarsssyndicator/?feed_id=1&format=raw

is the old RSS Syndicator to which many users have subscribed and 'pull' the site's latest articles from.

Since I'm upgrading the site to a newer platform and also changing the RSS Syndicator, the new RSS is the second URL:
/index.php?option=com_obrss&task=feed&id=2:kindykids-feed&format=html

This is why I need to redirect (or rewrite) the first url to the second, so users requesting RSS feed from the first url will be directed to the new URL.

If you are able to help me out and provide me with the necessary line of code, it would be much appreciated.

Many thanks.

Chris.
8:12 pm on Apr 7, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11846
votes: 242


you probably want a redirect and in either case you must use the RewriteRule and RewriteCond directives rather than Redirect.
8:56 pm on Apr 7, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


The "Redirect 301" form isn't incorrect, it just won't work here. You have to use mod_rewrite ("RewriteRule..." syntax), which looks at query strings.

This in turn-- domino effect --means that you have to change any existing redirects from mod_alias to mod_rewrite to avoid conflicts and things happening in the wrong order.

the new RSS is the second URL:
/index.php?option=com_obrss&task=feed&id=2:kindykids-feed&format=html

Surely there's a plugin that will fix this for you? The new URL is worse in so many ways than the old URL :(
10:00 pm on Apr 8, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


I'm guessing this is a Joomla site.

You really should change the Redirect directives to use RewriteRule.
4:27 am on Apr 9, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11846
votes: 242


I'm guessing this is a Joomla site.

You really should change the Redirect directives to use RewriteRule.

this translates technically to:
you are necessarily using mod_rewrite for joomla to work and therefore your use of mod_alias is going to be problematic.

http://httpd.apache.org/docs/current/rewrite/avoid.html [httpd.apache.org]:
when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.

this will typically expose your internal urls from rewrites to external redirects.
7:54 am on Apr 9, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


They're amazingly deadpan about when (not) to use mod_rewrite, aren't they? That same page-- unchanged between 2.2 and 2.4-- says almost at the top, emphasis mine:
The most common situation in which mod_rewrite is the right tool is when the very best solution requires access to the server configuration files, and you don't have that access. Some configuration directives are only available in the server configuration file. So if you are in a hosting situation where you only have .htaccess files to work with, you may need to resort to mod_rewrite.

That covers, hm, at least 90% of all websites in the world, doesn't it?
8:10 pm on Apr 9, 2014 (gmt 0)

New User

5+ Year Member

joined:Apr 6, 2014
posts: 3
votes: 0


At this particular point, I'm confused as to what can and what can't be done..... Is what I am asking for possible or will it require the use of mod_alias, causing serious security issues for the joomla website ?
9:29 pm on Apr 9, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


There is absolutely nothing you can do in mod_alias that can't be done just as easily in mod_rewrite.* So no worries there. An ordinary Redirect can be cleanly translated to a conditionless RewriteRule with [R] flag.

The main issue in a CMS is that you have to make sure any additional RewriteRules integrate cleanly with the material that's already there. Things have to be in the right order and you have to avoid conflicts.


* Yes, OK, there's the whole other side of mod_alias. The part involving, ahem, aliases. But you don't use those in htaccess.