Forum Moderators: phranque

Message Too Old, No Replies

REdirecting with parameters

         

empra6or

6:51 am on Oct 2, 2010 (gmt 0)

10+ Year Member



Right now I am hiding the extensions of my PHP files in order to make the links look nice.

Visitors use the following link:

example.com/news


to reach

example.com/index.php?page=news


The problem is that I am not able to use another parameter(s) to show articles by id. My goal is able able to do

example.com/news?id=34


At this point, PHP does not recognize the id parameter. Here is what I currently have in my .htaccess file


Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]



Any help would be great
Thanx

sublime1

3:50 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



Check the docs for the Query String Append [QSA] flag which is designed to handle this case.

Tom

empra6or

4:41 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



Not sure how do so. Would you please give me some examples.

Thanks

empra6or

5:14 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



Ok, this is what I have so far in my .htaccess

Options +FollowSymlinks
RewriteEngine on

# catch request with no querystring like /Home
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

# make other requests with a non-empty query string go to /index.php?id=#*$!XX
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^$ /index.php?id=$1 [QSA,L]

Now, when visitor go to (www.example.com/news) they can see the page
but when they go to (www.example.com/news?id=12) they get a 404 error

Any help would be great. Thank you everyone

g1smd

5:50 pm on Oct 2, 2010 (gmt 0)

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



In your second block of code, $1 will always be undefined/empty.

Make your real URLs of the form example.com/news/12 and you can do this thing a lot more easily.

I don't understand your example URLs. Are you stopping using 'news', and using numbers instead?

empra6or

6:14 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



I got it to work. The simplest solution was to just combine the two rules, since I can use the QSA flag to do the work of appending the id=# part for me:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [QSA,L]

jdMorgan

6:33 pm on Oct 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

# internally rewrite requests for URL-path "/xyz" with empty query string to filepath /index.php?page=xyz
RewriteCond %{QUERY_STRING} =""
RewriteRule ^([^/.]+)$ /index.php?page=$1 [L]
#
# internally rewrite other requests for URL-path "/xyz" with a non-empty query string to filepath /index.php?<existing-query-string>&id=xyz
RewriteRule ^([^/.]+)$ /index.php?id=$1 [QSA,L]

Note that the pattern in the second rule prevents recursion, since it will not accepts either a slash or a period in the requested URL-path. Without this or an explicit exclusion of "index\.php", this rule would create an 'infinite loop'.

The modified patterns will also prevent unnecessary rewrites of requests for 'real files' to your script. It is unlikely, for example, that you'd want to rewrite requests for robots.txt, sitemap.xml, image, document, multimedia, CSS, or JavaScript files to your script.

Jim