.htaccess url rewrite having issues - php

I am not able to customize a url using .htaccess. I applied all changes in .htaccess but this one stuck with me for long time.
I have a URL :
product.php?act=pro&cat=1&category=Nokia
I want to change it to:
product/1/Nokia.html
I used following rule:
RewriteRule ^product/?([a-zA-Z_]+)/([a-zA-Z_]+)\.html$ product.php?act=$1&cat=$2&category=$3 [QSA]
But not getting this one correct.

You're capturing only 2 groups and using $1, $2, $3.
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^product/([^/]+)/([^.]+)\.html$ /product.php?act=pro&cat=$1&category=$2 [QSA,L,NC]

Try this:
RewriteRule ^/product/([0-9]+)/(.*).html$ ^/product.php?act=pro&cat=$1&category=$2 [NC,L]

Pattern that matches cat should have 0-9 in it

Related

Mod_rewrite handling extra get variables (QSA not working)

I have a mod rewrite rule for settings page: (localhost/settings/index.php)
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [QSA,L]
And I am handling menu references within the URL which will be similar to:
http://localhost/settings/xyz/?ref=menu1&abc=2&de=3..
However, with my current rewrite rule, the variable does not get passed along and I didn't get the values of ref,abc..
I read here about QSA flag but that doesn't seem to be working.
What am I doing wrong?
Try turning off MultiViews option:
Options -MultiViews
RewriteEngine On
RewriteRule ^settings/([\w-]+)/?$ settings/?path=$1 [QSA,L,NC]
In place of QSA, you can use this trick to captured query string:
RewriteRule ^settings/([\w-]+)/?$ settings/?path=$1&%{QUERY_STRING} [L,NC]

htaccess Rewrite for clean URLs 2 get variables

Url rewriting doesn't seem to work.
I want to rewrite http://www.domain.com/files.php?key=file&id=10 to file/10
So this is the code I wrote in my .htaccess file:
RewriteEngine On
ReWriteRule ^(.*?) files.php?key=$1&id=$2
Doesn't seem to work. Anyone having a idea why?
You need two groups to use $2. Try
RewriteEngine On
ReWriteRule ^([^/]+)/(\d+)/? files.php?key=$1&id=$2
[^/]+ Means one or more symbols each of them isn't slash
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z]+)/([0-9]+)/$ files.php?key=$1&id=$2

create SEO friendly URL using rewriterule

I try to rewrite my SEO URLs to some real GET requests, to handle in my PHP file.
I want to have these 2 cases to work:
mysite.com/company-profile -> index.php?action=company-profile
mysite.com/faq/howcanijoin -> index.php?action=faq&anchor=howcanijoin
I got the first case to work using the rule:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?action=$1
For the second I tried also. I put this rule before the previous one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2
But it's not working. Any suggestions? If I understand correctly inside each parenthesis goes variables $1, $2 etc?
Do this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?action=$1&anchor=$2
This?
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2 [QSA,L]

Using Mod Rewrite to rewrite a url?

I have the following format:
http://www.mydomain.com/view/product/ID/CAT/TITLE
ID and CAT are both numeric. So, some real examples:
http://www.mydomain.com/view/product/1/2/ipod
http://www.mydomain.com/view/product/3/4/40-inch-tv
etc
I want to know if I should try to use mod rewrite, and how, or if I should rather fix the PHP to handle the paths correctly. I want to map the above to something like:
http://www.mydomain.com/CATEGORY/SUB_CATEGORY/TITLE
Any ideas?
RewriteEngine on
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)$ /$1/ [R]
RewriteRule ^([A-Za-z0-9]+)$ index.php?cat=$1
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/$3/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2&title=$3
in php you can get the variables as you would normally do, also you will need a rule for all variables entered and/or only category or sub-category or you will end up with a 404 when someone tries to access those pages
I would say that your question is not totally clear to me. Going by your examples what do you want your target URL to become from http://www.mydomain.com/view/product/1/2/ipod.
Will it be: http://www.mydomain.com/2/1/ipod ?
If yes then enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^view/product/([0-9]+)/([0-9]+)/(.*)$ $2/$1/$3 [L,R,NC]

URL Mod-Rewrite

I currently have a working URL:
http://example.com/security-services.php?service=fixed-camera-surveillance
and then I have PHP say, $_REQUEST['service'] to do some stuff...
But I'd like to achieve the same function if the URL looked like this:
http://example.com/security-services/fixed-camera-surveillance
Thanks!
An .htaccess file with something like this should do it.
Options +FollowSymLinks
RewriteEngine On
RewriteRule security-services/(.*)/? security-services.php?service=$1 [L]
The part that says security-services/(.*)/? matches the URL in the browser and rewrites it to security-services.php.
The key part is the (.*) which captures that portion of the URL and passes it to the PHP script as a GET value.
Try this rule:
RewriteEngine on
RewriteRule ^security-services/([^/]+)$ security-services.php?service=$1 [L]
The [^/]+ describes the next path segment after /security-services, (…) forms a group that’s match then can referenced to with $1 in the substitution.
And if you want a more general for any kind of …-service.php file:
RewriteEngine on
RewriteRule ^([^/-]+)-services/([^/]+)$ $1-services.php?service=$2 [L]
You can create a rule like this:
RewriteRule ^security-services/(.*)/? /security-services.php?service=$1
If you're using a .conf file, change ^ to ^/

Categories