Clean url in php with two get parameters - php

I am working on a PHP script that uses clean URIs.
My problem is, that I have one page that first uses no get parameter, then one and at the end two.
The line in the .htaccess file currently looks like this:
RewriteRule ^birthing-records/([^/]+)/?$ birthing-records.php?url=$1 [L,QSA,NC]
But if I add the second parameter like this:
RewriteRule ^birthing-records/([^/]+)/([^/]+)/?$ birthing-records.php?url=$1&second=$2 [L,QSA,NC]
The script redirects me to the error page.
How do I have to set this up?
Do I need two lines in the .htaccess for that case?
I would normally solve this by simply calling another page but I would like to keep the exact URIs I am using right now because all of those pages are indexed at Google. I would really appreciate any help.

You need one rule for each, otherwise whenever you put one param it'll break.
RewriteRule ^birthing-records/([^/]+)/?$ birthing-records.php?url=$1 [QSA,NC]
RewriteRule ^birthing-records/([^/]+)/([^/]+)/?$ birthing-records.php?url=$1&second=$2 [L,QSA,NC]
Thanks to #Martin for this note: that the L option of the first one has been removed since the L option indicates the last rule to be run (only one rule can exist with the L option)
Otherwise, as far as I can tell, they each work fine individually, but if you want to accept 1 OR 2 parameters, then two rules is what you need.
You can test htaccess stuff here: http://htaccess.mwl.be/

Related

Using a backreference at the beginning of a pattern in an apache rewrite rule

I'm currently translating a website from english to spanish. I want to use a rewrite rule that can put the current language at the beginning of the url, just after the domain. Example:
current url without rewrite rules:
http://www.example.com/sample_subdirectory/?language=spanish
objective:
http://www.example.com/spanish/sample_subdirectory/
At the time of this writing, I have all my php files rewritten as if they were subdirectories (such as example.com/something.php to example.com/something/) so there's no problem there. My objective though is to see if there's a way to rewrite the url so that instead of displaying the language GET variable, it displays "/spanish/something/something_else/etc/"; The goal is to not have to do an individual rewrite rule for every url just for the spanish translations, but rather append that /spanish/ subdirectory towards the beginning of the url. I tried to figure out how to use the metacharacters but I don't think I was doing it right. Here's what I tried (I'm pretty sure this is done so wrong :/ )
RewriteCond %{QUERY_STRING} ^id=(spanish)$
RewriteRule ^/spanish/(.*)$ /$1?id=%1
I'm still kinda new at rewrite rules, I only know basic stuff, I'm definitely not a web server administrator so I wouldn't know how to implement what I'm going for. Hopefully it's possible to do this. Ironically, I could have probably finished typing simple rewrite rules instead of spending an hour trying to do this but oh well :P
Based on the example you give above, the following should do what you want:
RewriteCond %{QUERY_STRING} ^language=spanish$
RewriteRule ^/(.*)$ /spanish/%1?
The RewriteCond causes the RewriteRule to be run only when the query string is "language=spanish".
The RewriteRule saves the directory from the original URL and appends it to the directory "/spanish/". The ? at the end removes the query string.
Thanks to bobdye's insight, I realized I had to remove the condition altogether and tweak the RewriteRule to look like this:
RewriteRule ^/subdirectory/spanish/(.*)$ /subdirectory/$1?language=spanish
Note: /subdirectory is a folder inside a virtual server (localhost) that I have running on my laptop.
As you can see, I had to place the /spanish/ directory in the pattern and whatever subdirectory was under that I would use as a back reference to the actual link, then I would just append the GET variable language=spanish to the end of the link. I'll still need to test to make sure that this works for any number of subheadings under /spanish/ but this definitely solved my problem.

Mod_rewrite question mark not appearing in output url

I'm trying to setup a simple mod_rewrite to redirect some open cart links which get broken when an import is done. Essentially the pretty urls for information pages don't go to where they should so im planning on overwriting them using .htaccess
Anyway I have a setup like so:
RewriteEngine On
RewriteBase /myshop.co.uk/
RewriteRule ^about-us/why-shop/?$ index.php?route=informationin/formation&information_id=7 [L]
So as I understand the following
www.myshop.co.uk/about-us/why-shop
will display the page contents of
index.php?route=information/information&information_id=7
However when I test this its only returning
www.myshop.co.uk/index.php
Everything after and including the ? appears to be ignored, first I thought I had to escape it but after reading various posts it doesn't seem like I need to.
Also while I'm here, is it possible to specify a RewriteBase which includes sub directories? For example
RewriteBase /development/myshop/
Everything after and including the ? appears to be ignored,
No that's correct it is appending full URL as per your rule. Only thing is that since you're not using R flag this redirection is happening silently.
If you add R flag in your rule then you'll notice full URL being displayed in your browser:
RewriteRule ^about-us/why-shop/?$ index.php?route=informationin/formation&information_id=7 [L,R]
Reference: Apache mod_rewrite Introduction

htaccess sequential redirection

I'm new with the htaccess redirection, and I'm having some trouble with how it works, I think that is because I need something a little bit different.
I want this
http://localhost/webroot/type_enduro-cross-trial/brand_yamaha/
to be redirected to
http://localhost/webroot/search.php?type=enduro-cross&trial=brand_yamaha
I use that values as filters from products; but the original URL is generated in two steps.
Example:
The user goes to the main page (http://localhost/webroot), and wants to filter by type, so when he clicks, the filter is applied and the URL changes to http://localhost/webroot/type_enduro-cross-trial and it works!
But I want the user to be able to add more filters, so now he wants to filter by some brand, and when he click the URL changes to http://localhost/webroot/type_enduro-cross-trial/brand_yamaha/. And so on with more filters.
This rewrite rule works to obtain the value of the first parameter:
RewriteRule ^([^_/]+)_([^_/]+)/([^_/]*) search.php?$1=$2 [L]
But when I add more filters, it clearly ignore them, and I have no idea of how must the rewrite rule should be.
Thanks, and sorry for my English!
What you want to achieve is non-trivial to do with .htaccess. Instead of solving this complicated within .htaccess just only leave a single rule inside the file and do the work inside your search.php script. This allows you a greater flexibility.
Just pass everything as a single parameter to the php script, like:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
In your php script you do some string processing on $_GET['page'] (exemplary from the example RewriteRule above), e.g. by using explode.
Inside PHP you can far better work with arrays, something you can not at all inside the .htaccess file.
Examples have been taken from the original Apache HTTP Mod_Rewrite documentation.

Mod Rewrite with Pagination

I wanna make this rather simple to ask so I can hope for a simple response. I'm somewhat new to mod rewriting (most I've done is a small cms using index.php?page=x and mod rewriting to that name). I have a shopping cart created by foreign people for my company before I started working here with little to no documentation and they are asking me to make the cart search engine friendly. I won't get too dirty with the details, just need to ask a question.
I have, say, results.php?name=friendly-url. I've edited my .htaccess so I can access these pages with a friendly url. It works perfectly.
RewriteRule ^([A-Za-z0-9-]+).html$ results.php?name=1
Now the cart has it written to paginate kinda awkwardly only because the $_GET variable is stupidly named. I'm trying to find out, without having to get really dirty and having to re-name files or re-route directories in the code, to make a simple friendly pagination.
The end result I'm looking for is something like starter-kits-01.html and starter-kits-02.html and so on. This is the mod rewrite I've been trying just to get something to work.
RewriteRule ^[A-Za-z-0-9-]+).html?p=([0-9]+)$ results.php?name=$1&pageNum_rsCWResults=$2
That, I believed, should allow me to render starter-kits.html?p=2. I'm getting no mod rewrite error, but it's messing up my $_GET variables. I can't do, say, /starter-kits/2/ without getting dirty and having to go through this messy code the foreign people made and change 500 lines of directories.
I've spent about 30 minutes on it, and I have 3 other projects going on today, so I'm going to move onto those while I wait for somebody a little more experienced with mod rewriting to give me a helping hand.
Much appreciated.
Just use:
RewriteRule ^([A-Za-z0-9-]+).html$ results.php?name=1 [QSA]
The QSA part tells it to forward any GET parameters on to the rewrite.
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
The page rewriting you need to do should be done in PHP itself and only require a few lines in mod rewrite. I really recommend you download a copy of Drupal or WordPress to see how they do it. But basically here is how it should work.
You create a URL structure like this:
product-search/cat-toys
product-search/cat-toys/page-1 (should point to the same place as the previous URL)
product-search/cat-toys/page-2 (could also use "product-search/cat-toys/page/2)
You take your site and have everything relay through a central index.php file, mod rewrite takes care of this. You just use the URLs on your site and the PHP will take the params passed and parse it into the URL structure that then takes you to the results.
Essentially the URL path is passed to index.php to one parameter by mod rewrite.
Sample mod rewrite entry (from Drupal, very similar to Wordpress):
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Please check out these systems to get a better idea. WordPress is a much simpler, more straightforward system to go through.

How do you make .htaccess pass all GET parameters without knowing what they are named?

I am working on a CMS that involves custom user pages. I have a hierarchy with three levels set up, and it works pretty well. It's got a .htaccess that makes the URLs friendlier, too:
RewriteRule ^([A-Za-z0-9]+).html /content.php?page=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+).html /content.php?page=$2 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+).html /content.php?page=$3 [L]
That code works just fine.
Another feature that I have is adding custom php page, in case I need to add something more powerful (like make a contact form that gets email addresses from a database).
So my question is how could I modify (or add to) the .htaccess to make all of the GET parameters passed to the PHP page. For example, to use the contact page example, if the user goes to http://www.mysite.com/contact.html?name=someuser, I want the .htaccess to rewrite it as content.php?page=contact&name=someuser, but without specifically telling it to look for 'name' as the parameter name. I would also like it to work with as many parameters as possible.
I know this is a big (and hopefully not confusing) request, but is it possible? Or at least parts of it? Thanks!
add QSA to your parameters. so for your first rule it would become
RewriteRule ^([A-Za-z0-9]+).html /content.php?page=$1 [L, QSA]
You'll need to use the QSA (query string append) flag. The mod_rewrite documentation has more detail.

Categories