.htaccess RewriteEngine not working for secondary GET - php

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]+

Related

How to redirect everything after slash to domain before?

This is what I'm trying to do:
I want example.com/car/{whatever} redirected to example.com/{whatever}.
For example, example.com/car/honda should be redirected to example.com/honda
BUT if there is nothing after /car/, then just leave it as that.
How can i do that in .htaccess ?
Removing a prefix from a URL is pretty simple, just
RewriteRule ^car/(.*)$ /$1 [R,L]
In order to keep a special case, you must put it in front
RewriteRule ^car/$ - [L]
This rule says, if there is just car/ leave it alone, see RewriteRule substitution
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

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]

CodeIgniter Mod Rewrite Rules and the Controller

Learning PHP, I am playing around with mod_rewrite and CodeIgniter. I have configured my .htaccess file correctly with
RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]
I understand a bit of regex, and can appreciate what happens here. The rewrite rules are applied and the server than handles the final URL which in the above case- attaches index.php (the front controller) to the "pretty" URL. So far so good.
I now want a URL pattern :
/<person-name>/at/<place>
to get translated to :
/index.php/person/list?personName=$1&place=$2
And i handle the request at my list function in the person controller. I do not understand why the following doesn't work:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/list?personName=$1&place=$2 [L]
What am i doing wrong/where is my understanding flawed? I see that the placeholders are extracted correctly ($1 and $3), however, it throws a CodeIgniter 404.
Many thanks!
It's possible that the simplest fix will fix your issue. By wrapping "at" in parentheses, you're creating another matching group, which means that $2 will always be "at." That could be breaking everything. You want index.php?person/list?personName=$1&place=$3 But you may have noticed that issue and fixed it without fixing the problem, in which case, read on.
Check out How to make CodeIgniter accept "query string" URLs?. It seems to indicate that you can't mix and match the segment-based approach and the query string approach. Without seeing your controller code, I can't say for certain, but I'd start investigating there. You might also try:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php?person/list/$1/$3 [L]
which would do the same thing the general-purpose CI redirect rule below does; send the URL off to index.php as a query string for processing. You've said you got it working with routes, so rather than passing a query string to your controller, you can expect person and place as two arguments. CI's handling of query strings leaves a lot to be desired, and I've never tried to MOD_REWRITE something including a query string into a query string.

URL rewriting is not working

I would like to use URL rewriting to a website.I had placed an .htaccess file in the server and turned on rewrite mode on and it seems to be working except one issue that I'm having.
I have two php extension files namely category.php and products.php resp.
Here is my requirement, the category.php should be called when one condition is met and products.php should be called another condition is meet
.HTACCESS:
RewriteRule ([A-Za-z-_]+) category.php?arg=name
RewriteRule ^([A-Za-z-_'']+)/([0-9]+) product.php?arg=name&pid=id
So her are my website url
FOR CATEGORY PAGES
http://www.mysite.com/category1
FOR PRODUCT PAGES
http://www.mysite.com/product-name/101
So the problem is with second url rewrite condition i.e product page url.When i put the ur l in browser it goes to 404.Whereas 1 rewrite condition seemed to work.Please help to access the webpage in the above format.
Problem is that in regex's character class hyphen should be either or start or at end otherwise it needs to be escaped (it will represent range otherwise).
Both of our rules:
RewriteRule ([A-Za-z-_]+) category.php?arg=name
RewriteRule ^([A-Za-z-_'']+)/([0-9]+) product.php?arg=name&pid=id
are not using correct regex and will produce wrong results.
Replace your rules with this code:
RewriteRule ^([a-z_-]+)/?$ /category.php?arg=$1 [L,NC,QSA]
RewriteRule ^([a-z_'-]+)/([0-9]+)/?$ /product.php?arg=$1&pid=$2 [L,NC,QSA]
PS: I have made some more corrections in the rule to handle unexpected situations better.
You need to add boundaries to your regex and backreference your captured groupings:
RewriteRule ^([A-Za-z-_]+)$ category.php?arg=$1 [L]
RewriteRule ^([A-Za-z-_'']+)/([0-9]+)$ product.php?arg=$1&pid=$2 [L]

Detect Single Segment RewriteRule apache

I'm stuck in a very basic thing, I'm try to redirect a single segment url to a page
RewriteRule /([A-Za-z0-9]+)$ index.php?c=profile&val=$1 [NC,L]
this works fine for URLs like
sitename.com/myurl
But the problem is its also valid for URLS like
sitename.com/myurl/something
or
sitename.com/myurl/something/x/y/z
I want it to only work for single segment after hostname, if I use it with ^ sign at the start it don't work at all
Any help would be greatly appreciated.
Thanks
You still have to use ^, but must leave out the leading /
RewriteRule ^([A-Za-z0-9]+)$ index.php?c=profile&val=$1 [NC,L]
For compatibility with older mod_rewrite versions you often see idioms like ^/?. But current Apaches mod_rewrite strips the leading slash already (from the REQUEST_URI which RewriteRules operate from primarily).

Categories