• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1105
  • Last Modified:

ModRewrite URLs with Variables

Hi,

I'm trying to gracefully handle old urls with variables and redirect them to either a home page or a new corresponding (non-variable) url of my site.

An example composition of urls I have that I need to redirect:

shop/cart.php?target=category&category_id=102
forum/comments.php?DiscussionID=63

What should not matter is the actual value of the variable, because there are literally hundreds of them.  Ideally, the modrewrite should be a catch-all regardless of the variables value.

An example of a corresponding page for store pages will be:

/store/

while the forum/comments.php will simply go to the home page of the site.

I really appreciate the help.

Thanks!
0
pmagony
Asked:
pmagony
  • 11
  • 9
1 Solution
 
grahamnonweilerCommented:
Simple instructions in an .htaccess file:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^/?shop/(.*)$  ./store/ [R=301,L,NC]
RewriteRule ^/?forum/(.*)$  ./ [R=301,L,NC]

Open in new window

0
 
pmagonyAuthor Commented:
Thanks grahamnonweiler,

I tried your suggestion, but the links for the shopping cart are still coming up broken, they go from:

shop/cart.php?target=category&category_id=102

to

/store/cart.php?target=category&category_id=102

I want it to just go to /store/

Also, with the reference to forum links, here is he original:

forum/comments.php?DiscussionID=63

to

www.domain.com/?DiscussionID=63

it should not carry over the variables in both instances.

Thanks again!
0
 
grahamnonweilerCommented:
Simply add ? at the end of the rewrite. It will strip the QueryString off completely:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^/?shop/(.*)$  ./store/? [R=301,L,NC]
RewriteRule ^/?forum/(.*)$  ./? [R=301,L,NC] 

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
pmagonyAuthor Commented:
I'm sorry, that is not working. Here is what i have:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^/?shop/(.*)$  ./store/? [R=301,L,NC]
RewriteRule ^/?forum/(.*)$  ./? [R=301,L,NC]
0
 
grahamnonweilerCommented:
You wll need to use implicit redirects then, but normally the Query String is stripped if the ? is used followed by a space. Try the below, replacing "www.domain.com" with your domain:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^/?shop/(.*)$   http://www.domain.com/store/? [L,NC]
RewriteRule ^/?forum/(.*)$  http://www.domain.com/forum/? [L,NC]

Open in new window


If it does not work, post back here what your server is redirecting to
0
 
pmagonyAuthor Commented:
The end result, continues to be a redirect from shop to:

http://www.domain.com/store/cart.php?target=category&category_id=102

and

http://www.domain.com/?DiscussionID=63

Do we not need to specify in the RewriteRule the page of the shop and the page of the forum?  For example:

RewriteRule ^/?shop/cart.php(.*)$   http://www.domain.com/store/? [L,NC]
RewriteRule ^/?forum/comments.php(.*)$  http://www.domain.com/forum/? [L,NC]

Open in new window


?
0
 
grahamnonweilerCommented:
Strictly speaking the rewrite is removing everything after "shop" and putting it in to a variable that we do not use in the rewrite, however try:

RewriteRule ^/?shop/cart\.php$   http://www.domain.com/store/? [L,NC]
RewriteRule ^/?forum/comments\.php$  http://www.domain.com/forum/? [L,NC]

Open in new window

0
 
grahamnonweilerCommented:
You might have to add R=301 to the rewrite (for SEO etc):

RewriteRule ^/?shop/cart\.php$   http://www.domain.com/store/? [R=301,L,NC]
RewriteRule ^/?forum/comments\.php$  http://www.domain.com/? [R=301,L,NC] 

Open in new window

0
 
pmagonyAuthor Commented:
I do appreciate your effort, but it's still a no-go. Results are the same.
0
 
pmagonyAuthor Commented:
I even tried this and it is still producing the same results:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^/?shop/(.*)$   http://www.domain.com/store/? [R=301,L,NC]
RewriteRule ^/?forum/(.*)$  http://www.domain.com/forum/? [R=301,L,NC]

Open in new window

0
 
grahamnonweilerCommented:
The results can not be exactly the same.

The rewrite:

RewriteRule ^/?shop/cart\.php$   http://www.domain.com/store/? [R=301,L,NC]



will produced something like:


http://www.domain.com/store/?target=category&category_id=102

Correct?

While:

RewriteRule ^/?forum/comments\.php$  http://www.domain.com/? [R=301,L,NC]

will rewrite something like:

http://www.domain.com/?DiscussionID=63

Please confirm that the above is what is appearing in your browser address - and please use a browser to check what is happening.
0
 
grahamnonweilerCommented:
Finally, you could try the QSD (query string discard) flag although it is not widey used:


RewriteRule ^/?shop/cart\.php$   http://www.domain.com/store/? [R=301,QSD,L,NC]
RewriteRule ^/?forum/comments\.php$  http://www.domain.com/? [R=301,QSD,L,NC]
0
 
pmagonyAuthor Commented:
Hi Graham,

I can confirm the above, exactly as you have defined it.  The redirect DOES work, but the variables strings are still carried over.  In the case of the forum, it's not a terrible issue because it does resolve to the home page, but with the shop redirect to store, it does product 404 errors.

My goal is to strip out all the variables and the suggestions aren't working.  Let me give the final QSD flag a shot.  I'll report back in 2 minutes.
0
 
pmagonyAuthor Commented:
Adding QSD causes an Internal Server Error.  When I remove it, it goes away.

I tried this:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/?shop/cart\.php$   http://www.domain.com/store/? [R=301,L,NC]
RewriteRule ^/?forum/comments\.php$  http://www.domain.com/forum/? [R=301,L,NC]

Open in new window


and

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/?shop/(.*)$   http://www.domain.com/store/? [R=301,L,NC]
RewriteRule ^/?forum/(.*)$  http://www.domain.com/forum/? [R=301,L,NC]

Open in new window


And both redirected correctly, but the query strings were still appended.
0
 
pmagonyAuthor Commented:
Does this help? Fri Aug 31 06:44:56 2012] [alert] [client 0.0.0.0] /var/chroot/home/content/50/8888888/html/.htaccess: RewriteRule: cannot compile regular expression '^/?shop/(*)$'
0
 
grahamnonweilerCommented:
The error comes with QSD - or all the time?

The * should have a . (period) before it:

RewriteRule ^/?shop/(.*)$   http://www.domain.com/store/? [R=301,L,NC]

However, when we try the above rewrite on our Apache based servers the query string is stripped (without QSD) - which is how it should work.

Do you have anything else in your .htaccess file apart from your rewrite rules?

Do you know which version of Apache is running?
0
 
pmagonyAuthor Commented:
The error comes ONLY when QSD is present.  Yes, the .htaccess file has many Redirect 301 commands, it also has the following at the very top of the file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Open in new window


order allow,deny
	deny from xx.xxx.xxx.xx 
	deny from xx.xx.x.xx
allow from all

Open in new window


Server : Apache/2.2.3 (Red Hat)
0
 
grahamnonweilerCommented:
I am assuming you add your rewrites after line 7 - although I can not see what purpose line 4 serves, you are rewriting index.php to itself.

Also, if you have your .htaccess in your root folder (meaning the root of your website not Linux "root") then line 3 is not needed.

Using the same syntax as your existing rewrite rules please try:

RewriteRule ^/?shop/cart\.php$ http://www.domain.com/store/ [QSD,R,L,NC]

Please place it as the first rewrite rule.
0
 
pmagonyAuthor Commented:
YES............... you are the man.  Although, your RewriteRule didn't work as you provided, based on our previous attempts I was able to tweak it to this, and get it to work successfully:

RewriteRule ^/?shop/cart\.php$ http://www.domain.com/store/? [R=301,L,NC]

The key, as you kindly instructed was to put it as the first RewriteRule. I also took out the QSD as it causes the Internal Server Error, and changed the R to R=301.  I also added the ? at the end of the URL to strip out the variables carried over.

What a learning experience! Thank you! I will go ahead and do the forum one now as well.
0
 
pmagonyAuthor Commented:
Please read my response, as there were tweaks needed to get it to work.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now