rewriterule mixed regular query and clean URL? - php

I am trying to get a regular query appended to any clean URL to be parsed properly. I've spent the last 3 hours trying to attempt this to no avail.
/aa/bb/?f=ff
/aa/bb/cc/dd/ee/?f=ff&g=gg
should become
a=aa&b=bb&f=ff
a=aa&b=bb&c=cc&d=dd&e=ee&f=ff&g=gg
instead I get
a=aa&b=bb
a=aa&b=bb&c=cc&d=dd&e=ee
This is my htaccess line:
RewriteRule ^([a-z]+)/([a-z]+)/?([a-z0-9]+)?/?([a-z0-9]+)?/?([a-z]+)?/?\??(.*)?$ index.php?a=$1&b=$2&c=$3&d=$4&e=$5&$6 [NC,L]
If you are wondering why I want to do this, it is to accommodate some libraries which do not support clean URLs very well.

Use the QSA flag, eg
RewriteRule ^...$ foo.php [NC,L,QSA]

Related

Simple pretty urls with .htaccess

I have a one page blog php website.
Content is dynamicaly loaded based on get parameters. I would like to use my htaccess to make pretty urls. I have these urls:
website.com/index.php?category=review&page=1
And I would like to have this:
website.com/category/review/page/1
And I also use article as get parameter. So I would like to change this:
website.com/index.php?article=12345-name-of-article
To this:
website.com/article/12345-name-of-article
I am totally new to htaccess, so any help would be appreciated.
I tried this rewrite rule:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ index.php?article=$i [NC,L].
It worked somehow, but php script does not recognize url parameters. So it does not work.
Thank you very much!
You need to use QSA - When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined. :
RewriteEngine On
RewriteRule ^article/([\w-]+)(?:\.html|/)?$ index.php?article=$1 [NC,QSA,L]
RewriteRule ^category/([\w-]+)/page/([\w-]+)(?:\.html|/)?$ index.php?category=$1&page=$2 [NC,QSA,L]

.htaccess RewriteEngine not working for secondary GET

I'm creating friendly links in my site, but I have a problem. I wrote this, but only the first line of code is working, and I'n not sure why:
RewriteRule ^user/([0-9a-zA-Z]+) user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options user.php?user=$1&options [NC,L]
When I go to test.com/user/tom everything is fine, but when I try test.com/user/tom/options it just loda the same page.
I have php script that should load different pages, for different $_GET, and its working if used with normal links.
The correct thing to do here is not to swap the lines around. Whilst that may fix your problem, it will also send user/tom/foo/bar to the first rule. The reason it does this is because the pattern is not closed with $. As such, your rules should read as follows:
RewriteRule ^user/([0-9a-zA-Z]+)$ user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options$ user.php?user=$1&options [NC,L]
Tip: If you also have other pages for the user, you can simplify the second rule to include different segments by means of a capture:
RewriteRule ^user/([0-9a-zA-Z]+)/(options|other-page)$ user.php?user=$1&$2 [NC,L]
Also giving consideration to the fact that you are using the NC (no case) flag, you can drop the A-Z in your capture groups. You can also use \d for digits. Example [a-z\d]+

URL rewriting with spaces

I have a url like this :
www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe
I am trying to conver this like below with the help of .htaccess file :
www.qwerty.in/1/abcd-cafe
I am trying the below but unfortunately its not working . can anyone help
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ details.php?vendor_id=$1&name=$2
You character must allow space as well:
RewriteRule ^([\w-]+)/([\w-\s]+)$ details.php?vendor_id=$1&name=$2 [L,QSA]
I have used \w, which is same as [a-zA-Z0-9_].
The following rule will convert your URL from www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe to www.qwerty.in/1/abcd%20cafe:
RewriteRule ^([^/]*)/([^/]*)\.html$ /details.php?vendor_id=$1&name=$2 [L]
It it much more generalized however, and will convert the first two parameter values even if they are not vendor_id or name. If this is not the behavior you'd prefer, have a look at anubhava's answer.
Converting a space to underscore can be achieved using:
RewriteRule ^(.*)\s(.*)$ $1_$2 [N]
Also, don't forget to ensure the rewrite engine is online:
RewriteEngine On
If you want to further experiment with these rules, I'd recommend a website I recently discovered: http://www.generateit.net/mod-rewrite/

Paging URL rewriting not working in php using .htaccess

I am using this rule in .htaccess :-
RewriteRule ^online-sale on-sale.php
RewriteRule ^online-sale/page-([0-9]+)$ on-sales.php?page=$1
First rule is working fine. for eg. if i call http://www.sitename/online-sale than page is opening successfully. When i am calling http://www.sitename/online-sale/page-2 than page is opening fine, but I can't access $_REQUEST["page"] value on this page.
Can anyone suggest me what is the problem? Is it possible or not?
Thanks in advance.
You need to use anchor $ in first rule to avoid matching for paging URL as well:
RewriteRule ^online-sale/?$ on-sale.php [L]
RewriteRule ^online-sale/page-([0-9]+)/?$ on-sale.php?page=$1 [L,QSA]
It is also advisable to use L and QSA flags.
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Conditional second query in rewrite rule

Currently I have a rule in the .htaccess file that makes a shorter URL:
RewriteRule ^job(.*)$ /include/myfile.incl.php?proj=$1 [NC,L]
to make the final URL looks like:
mydomain.com/jobXXXXXX
Is it possible to accommodate second query is a user changes my URL like this?
mydomain.com/jobXXXXXX?token=123
I need to be able to pick-up that token value too then.
Perhaps there's a way to add a conditional $2 somehow? I've tried QSA flag but it did not work for me. Perhaps my Apache version is too old... 2.2.3(CentOS)
Whichever solution I could use, another rewrite rule or perhaps in combination with PHP. I'm stuck.
UPDATE
NM... I think the problem is in my PHP, not .htaccess
Are you talking about QSA flag?
RewriteRule ^job(.*)$ /include/myfile.incl.php?proj=$1 [QSA,NC,L]
The QSA flag should work. I'm using it 5 years now.
Use %{QUERY_STRING} in your rewrite target to access the query string. Something like:
RewriteRule ^job(.*)$ /include/myfile.incl.php?proj=$1&%{QUERY_STRING} [NC,L]

Categories