1

I'm struggling to implement a URL change on a site. Firstly I'm wanting to only implement this URL change at a subdirectory level.. I have WordPress installed at the root but I'm in the process of creating a custom classifieds section. This sits in the directory /classifieds/. As a result I only want to rewrite URLs in this subdirectory.

I've managed to get the rewrite working correctly for just the classifieds subdirectory as required with a 301 performed and trailing slash added for SEO. The only problem I've got is none of my pages are passing query string values. How can I modified my .htaccess code below so it will enable querystrings to be passed between pages?

IndexIgnore *

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mysite.com/classifieds/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://www.mysite.com/classifieds/$1/ [R=301,L]

redirect 301 /classifieds/index/ http://www.mysite.com/classifieds/

Thanks in advance for all help!

Chris
  • 31
  • 1

2 Answers2

0

You need to add [QSA] Query String Attach

EDIT

RewriteRule ^(([^/]+/)*[^.]+).php$ http://www.mysite.com/classifieds/$1 [R=301,L,QSA]

and

RewriteRule (.*)$ http://www.mysite.com/classifieds/$1/ [R=301,L,QSA]

ianbarker
  • 1,255
  • 11
  • 22
  • I am very much a beginner could you advise which lines in the code require [QSQ] adding please... – Chris Feb 02 '12 at 13:10
  • I've tried your suggestion, but I'm still running into difficulties. With [QSA] added I get the following URL output:http://www.mysite.com/classifieds/view-ad/?user=0001 – Chris Feb 02 '12 at 14:18
  • 1
    Ian, how does [QSA] help? All this does is to instruct the rewrite engine to append existing the query_string when the replacement string contains a `?...` – TerryE Feb 02 '12 at 15:30
  • Sorry, I thought that was the issue. I'm going home now, but I'll have another look at this tomorrow if nobody else has solved it – ianbarker Feb 02 '12 at 17:21
  • @ianbarker Much appreciated if you could have another quick look, everything I try just seems to break something else. Thanks again! – Chris Feb 02 '12 at 21:52
  • @Chris could you provide an example URL and an example of what you are expecting it to be after it's rewritten? – ianbarker Feb 03 '12 at 13:39
  • @ianbarker sorry for the delayed response been a busy day in the office, hope this reaches you before you leave: This may seem like a basic question but which of the below is the best way to pass a query string on a shortened URL: mysite.com/classifieds?user=1 or mysite.com/classifieds/?user=1/ or mysite.com/classifieds/?user=1 Whichever is the best method I want to use to pass the querystring. All of the methods I have tried so far fail to pass the query string. Hope I'm making some sense :) – Chris Feb 03 '12 at 16:32
  • @ianbarker I simply want to pass a query string on a SEO friendly / shortened (file extension removed) URL! – Chris Feb 03 '12 at 16:56
  • @Chris I'm still confused, which is easily done. So you want to capture something from the querystring and put it into the url? – ianbarker Feb 03 '12 at 17:06
  • @ianbarker I want to modify my htaccess to output shortened URLs but also so it will allow query strings to be passed. Would be much easier to just give you the url :) – Chris Feb 03 '12 at 17:24
0

If I'm understanding this correctly, you want the query string to persist through multiple pages, right? If so, the only way I know of to do this is going to be to output all links on the page with the query string appended, meaning this will be done in you PHP file.

To do this, you links should be output with urlencode($_GET) appended to the end of the url.

Dan Ambrisco
  • 865
  • 7
  • 13