Transform Regex to RewriteRule - php

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]

Related

.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/

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]

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