Redirecting to SEF page if a particular link is hit from browser

Apache gurus,

I am having a small trouble it is only because of it is complicated but because of my less knowledge on apache configuration and mod_rewrite (regex).

I just need to redirect the user to another link which is of same server/site i.e. if a user hits the following url in the browser:
http://www.mysite.com/index.php?option=com_fashion&id=1&Itemid=2
then the user should view (or should be redirected to) the following page:
http://www.mysite.com/abc/xyz/article-alias (or so).

I just tried simple way:


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php?option=com_fashion&id=1&Itemid=2 http://www.mysite.com/abc/xyz/article-alias [L, R]

But it does not seem to work.

Can anyone please help me or point to the right direction?

Help will be much appreciated!

Thanking you in advance.

Regards
Raju Gautam

Rajug,

Your code is syntactically wrong:

RewriteRule regex redirection [flags]

where regex is matched against the {REQUEST_URI} variable (which does NOT include the query string). The redirection is fine (except that it does NOT look like something Apache can serve) and you CANNOT have a space in the flags!

May I recommend that you visit the tutorial article linked in my signature?

Regards,

DK

:blush:
What I had worked before with is without the query string or I had written few lines before for something SEF URLs reading your article in sitepoint which was SEF urls to normal. But this time I am trying is different (and difficult to me) because I need to write something from normal url to sef urls so I am confused.

For now the alias or the URL to be redirected is fixed and the given URL was just an example. :slight_smile:

I tried as you suggested but I don’t know what I am still doing wrong. I just tried to redirect to google search page with the condition but it does not seem to work: :frowning:


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} option=com_fashion&id=1&Itemid=2
RewriteRule .? http://www.google.com [L,R=301]

What is wrong with the above code Dk? I have read your article before but only till my requirement fulfilled. I will try to read the whole articles and examples once soon.

For now can you please correct on my code above if anything is wrong?

Thank you so much DK once again for taking care my post.

Regards
Raju Gautam

Raju,

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} option=com_fashion&id=1&Itemid=2
RewriteRule .? http://www.google.com [L,R=301]

That says, in order,

Take mod_rewrite out of comment mode then,

If the requested file does not exist AND
If the requested directory does not exist AND
If the query string is EXACTLY option=com_fashion&id=1&Itemid=2
Redirect EVERYTHING to http://www.google.com.

I should take time out here, though, to note that your stated redirection is OPPOSITE what should be done, i.e., if a visitor requests http://www.mysite.com/abc/xyz/article-aliashttp://www.mysite.com then the page serves should be http://www.mysite.com/index.php?option=com_fashion&id=1&Itemid=2. Remember, YOU create the links the way that you want visitors to see them and then YOU create the mod_rewrite code to convert the “viewed format” to the “serve-able format.” IF I read this correctly, then your code (for the original post) is simply:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} 
RewriteRule ^abc/xyz/article-alias$ index\\.php?option=com_fashion&id=1&Itemid=2 [R=301,L]

See the difference? Okay, abc, xyz and article-alias should be variables and backreferences to them should be used in the redirection but you’ve not shown me which go where nor did you respond to the characters allowed in each, i.e., ([-a-zA-Z0-9]+) to capture one or more of all letters, digits and dashes.

Regards,

DK

Thank you so much DK.

Well, first of all let me tell you the scenario why I want this OPPOSITE rule to implement in my case. The website was developed using Joomla CMS and a newsletter has been sent to the members/users. The client sent a newsletter to their members having a broken link (by mistake a link which points to a non-existing article/page) and the link is as I mentioned http://www.mysite.com/index.php?option=com_fashion&id=1&Itemid=2 and at that time the site was not using SEF URLs. Now the site has used SEF URLs and what client want is; if a member/user clicks the link in the newsletter he wants the user to be redirected to an existing and valid page of the website.

That’s it what it should work. But unfortunately the page does not redirect to google page :frowning:

Yes DK, I have understood those basic things about mod_rewrite from your article in sitepoint. I have bookmarked your article and I usually see that and make some basic mod_rewrite things in my works. For example:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-a-zA-Z0-9]+)/[-a-zA-Z0-9]+/[-a-zA-Z0-9]+$ index\\.php?param1=$1&param2=$2&param3=$3

I have used this type of rewrite for several times and they are working quite good too so far. But the condition this time I am falling in is totally different to me.

Thanking you once again for your valuable time and advice as usual.

Regards,
Raju

Raju,

Okay, excellent reason for that redirection (before the CMS you’re now using). However, please confirm the URI (not the domain - that’s not necessary).

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} option=com_fashion&id=1&Itemid=2
RewriteRule .? http://www.google.com [L,R=301]

That is checking for the existence of the file, directory and EXACT query string before redirecting everything to google. Thus, if the values of the keys are off, even minutely, even with just the order, this will NOT redirect.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-a-zA-Z0-9]+)/[-a-zA-Z0-9]+/[-a-zA-Z0-9]+$ index\\.php?param1=$1&param2=$2&param3=$3 [COLOR="Blue"][L][/COLOR]

Yes, that’s what you’re use - EXCEPT in your situation.

Regards,

DK