Conditional second query in rewrite rule - php

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]

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]

Transform Regex to RewriteRule

A few minutes ago I asked a question and received a very good response. It works. (This is it). I added it to my PHP scripts. Now I need to complete my site with another question like that but little characterized. I need to get rewrite rule from that regex (it's different problem, not my previous).
I need to rewrite link like:
http://my_site.com/technic/k-700/?type=repair
to link like:
http://my_site.com/repair/k-700/
Instead of k-700 can be any another combination (between / ) and instead of repair can be only kit.
Now I need rewrite rule for .htaccess file. Please.
My result is not working:
RewriteRule ^([^\/].)/k-700/$ /technic/k-700/?type=$1 [L]
I can't avoid that k-700 :(
If my understanding is correct and this is going in your root .htaccess, then try this:
RewriteRule ^(repair|kit)/([^/]+)/?$ technic/$2/?type=$1 [L]
UPDATE: Re-read your description. a little ambiguity with the repair vs kit value and whether substitution should always have k-700. So here are a few more examples catering to those possibilities:
RewriteRule ^(repair|kit)/([^/]+)/?$ technic/k-700/?type=$1 [L]
RewriteRule ^(kit)/([^/]+)/?$ technic/k-700/?type=$1 [L]
RewriteRule ^(kit)/([^/]+)/?$ technic/$2/?type=$1 [L]

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.

Confusion with .htaccess

I'm trying to wrap my tiny brain around how the .htaccess can convert my somewhat undesirable URL into a cleaner eye candy link.
This is my current, scruffy URL.
expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
And what I would like it to be render as, would be something perhaps like this?
expand/Mods/Wrestlers/WWE/
Am I right in thinking this is correct syntax to perform this? Because it doesn't seem to do anything right now!
RewriteRule ^([^/]+)/([^/]+)$ expand.php?p=$1&sp=$2 [L]
I would appreciate it if some bright chap might be able to help me out of this pickle!
You can do it like this. You will need to make sure the rewrite matches the URL you wish to internally redirect too, which is expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
So to take care of that you should be able to use this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+) expand.php?category=$1&subcategory=$2&faction=$3 [NC,L]
Try
^expand/(\w+)/(\w+)/(\w+)?$ expand.php?category=$1&subcategory=$2&faction=$3
To internally rewrite expand/Mods/Wrestlers/WWE/ to expand.php?category=Mods&subcategory=Wrestlers&faction=WWE, you'll need to make a regex that matches that first url. Your current regex would match an url with 2 parts, but your example url has 4 parts. I am pretty sure that doesn't fit ;-)
So... how do we fix it? Well, we make a regex with 4 parts. In fact, we know that the first part needs to be equal to expand, so we end up with this:
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+)/?$ /expand.php?category=$1&subcategory=$2&faction=$3 [QSA,L]
If you add this to the .htaccess in your www-root and we now go to http://example.com/expand/Mods/Wrestlers/WWE/, we should see whatever expand.php outputs with those parameters.

Can I use PHP and/or htaccess to rewrite a URL but not redirect?

Is there a way I can use PHP (and/or .htaccess) to rewrite the URL of a page.
For example if a user goes to www.mysite.com/french they are actually accessing the page which is www.mysite.com/index.php?lang=fr
But not a redirect.
You want to use mod_rewrite and an .htaccess file to achieve this.
RewriteEngine On
RewriteBase /
RewriteRule ^french/(.*)$ /index.php?lang=fr [L,QSA]
Yes, using Apache mod_rewrite and appropriate rules in an .htaccess file.
The docs on mod_rewrite are here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
on the Apache site you can find several examples of URL rewriting flavors, by the way it's enough to use something like this in an .htaccess file:
RewriteEngine on
RewriteRule ^french(/|)$ /index.php?lang=fr [flag]
Where [flag] can be one of the following:
[PT]
[L,PT]
[QSA]
[L,QSA]
You may want to have a look at the PT (passthrough) flag docs or RewriteRule flags docs.
Also, pay attention to what your links are pointing to: in fact, the RewriteRule first argument is a regular expression that will be used to match the URLs to be rewritten. At the moment,
^french(/|)$
matches "french", right after the domain name,followed either by a slash (/) or nothing (that's the meaning of (/|) ); that is, it'll match www.mysite.com/french and www.mysite.com/french/ but nothing else. If you need to parse query string arguments, or subpaths, then you may need a more complex regex.

Categories