short links with htaccess - two arguments - regex

I've got little problem that I am not sure how to fix. I am using htaccess file to rewrite my url to shorter links. For example I am using this rule:
RewriteEngine On
RewriteRule ^client/(.*)/?$ pages/client.php?action=$1 [L,QSA]
but I wanted to also pass second argument to that URL so I made this:
RewriteRule ^client/(.*)/(.*)/?$ pages/client.php?action=$1&view=$2 [L,QSA]
so I could access URL like:
/client/action_1/view_2
without any problems but when I skipped second argument and only accessed:
/client/action_1/
then it was impossilbe and resulted in 404.
How to make a change to that RewriteRule so I can access both:
/client/action_1/view_2
/client/action_1/
and it would rewrite to:
pages/client.php?action=$1&view=$2

You can have 2 separate rules to handle 2 clean URLs:
RewriteEngine On
RewriteRule ^client/([^/]+)/?$ pages/client.php?action=$1 [L,QSA,NC]
RewriteRule ^client/([^/]+)/([^/]+)/?$ pages/client.php?action=$1&view=$2 [L,QSA,NC]

Related

Is there any way to redirect to a clean url when old url is requested?

I want to change the URLs on my website from page.php?id=1&name=john to page/1/john using htaccess RewriteRule.
This is what I have currently but it is not working as expected:
RewriteRule ^page/([0-9]+)/([a-zA-Z\s-]+) page.php?id=$1&name=$2
Is it possible to make this change in htaccess rules or should I change every link to <a href='page.php/1/john'>Page</a> which is tiresome since I have got many links in every page. Help is appreciated. Thanks.
You may use these 2 rules in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /page\.php\?id=(\d+)&name=([^\s&]+)\s [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^page/(\d+)/([^/]+)/?$ page.php?id=$1&name=$2 [L,QSA,NC]
1st solution: To get from query string URL to user friendly url try following, as per OP's request.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^id=(\d+)&name=([\w-]+)/?$ [NC]
RewriteRule ^([^.]*)\..*$ /$1/%1/%2/ [QSD,R=302,NC,L]
2nd solution: As far as I get from OP's question, could you please try following. Considering as per thumb rule users will be given friendly URL like eg--> http://localhost:80/page/1/john and it will point in pointed to http://localhost/page.php?id=1&name=john in backend.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ /$1.php?id=$2&name=$3 [NC,L]

RewriteRule when there is ?id=

I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on

.htaccess RewriteRule outputs wrong parameter result

I am trying to use .htaccess RewriteRule to beautify some urls, but I've got a problem that I can't fix.
I've written this:
RewriteEngine On
RewriteRule ^(.*)/(foro|forum)$ code/forum/forum.php?id=$1
RewriteRule ^(.*)/(foro|forum)/(.*)$ code/forum/forum.php?id=$1&subf=$3
RewriteRule ^(.*)/(foro|forum)/(.*)/(.*)$ code/forum/forum.php?id=$1&subf=$3&post=$4
The first url rewrite works properly, showing as result the id parameter (I.e. localhost/web/user501/forum -> id=user501).
It happens the same with the second one (localhost/web/user501/forum/3 -> id=user501, subf=3).
But when I try the third url rewrite, somehow the output id is not 'user501' as I expect, but 'code/forum/forum.php'.
The output of the other vars is normal (localhost/web/user501/forum/3/5 -> id=code/forum/forum.php, subf=3, post=5).
I tried to change the first (.*) on that url rewrite for a fixed word... it worked, but unfortunately I need it to be a changeful word.
Does someone know what's happening?
That is because ^(.*)/(foro|forum)/(.*)/(.*)$ pattern also matches code/forum/forum.php hence RewriteRule runs again and gives you wrote results.
Try this code:
RewriteEngine On
# ignore /code/forum/forum.php
RewriteRule ^code/forum/forum\.php$ - [L,NC]
RewriteRule ^(.*)/(foro|forum)$ code/forum/forum.php?id=$1 [L,QSA]
RewriteRule ^(.*)/(foro|forum)/(.*)$ code/forum/forum.php?id=$1&subf=$3 [L,QSA]
RewriteRule ^(.*)/(foro|forum)/(.*)/(.*)$ code/forum/forum.php?id=$1&subf=$3&post=$4 [L,QSA]

Alter section of URL using htaccess

I'm trying to alter a part of an URL as below from this type URL ...
/shop/product/41/3030651/Apple-Imac-215-Inches-Me086ba-By-Viking.html
to this ..
/shop/product/1/030651/Apple-Imac-215-Inches-Me086ba-By-Viking.html
where all numbers like 41 are altered to 1 but it seems this code ...
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=product&proddb=$1&pid=$2&urltxt=$3 [L]
is interfering.
I've tried lots of rewrite configurations but nothing seems to work but I do get a reaction from the code below ...
RedirectMatch 301 /shop/product/41/(.*) /shop/product/1/$1
But it must conflict with the RewriteRule above as I get sent to the root of the site.
Have redirect rule before your earlier rewrite rule:
RewriteEngine On
RewriteRule ^product/41/(.+)$ /product/1/$1 [L,NC,R=301]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?case=product&proddb=$1&pid=$2&urltxt=$3 [L,QSA,NC]

Mod_rewrite - redirecting an already rewritten url

I had previously rewritten the URL for articles this way:
RewriteRule ^([^/\.]+)/([^/\.]+)/?article_(.*)_(.*).html$ index.php?sectionpp=$1&sectionpp2=$2&content=article&artid=$3&curbigart=$4 [L]
This produced the url like:
http://www.domain.com/Entertainment/114194/VINYETTE-TO-RELEASE-DEBUT-ALBUM-ON-MAY-7-2013
However, now I have redesigned the site and made the urls shorter:
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?sectionpp=$1&content=article&artid=$2&curbigart=$3 [L]
Which produces url like:
http://www.domain.com/Entertainment/114194
The QUESTION:
Is it possible to redirect the already rewritten url
http://domain.com/Entertainment/114194/VINYETTE-TO-RELEASE-DEBUT-ALBUM-ON-MAY-7-2013
to
http://domain.com/Entertainment/114194
And obviously we're not talking about single page, but thousands, so good one line in .htaccess would be needed hehe. I know I could simply redirect in php, but I assume there must be a way to do that in .htaccess as well?
Thanks!
Is it possible to redirect the already rewritten url domain.com/Entertainment/114194/VINYETTE-TO-RELEASE-DEBUT-ALBUM-ON-MAY-7-2013
to domain.com/Entertainment/114194
You can use rule as very first rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+/\d+)/.+$ /$1 [L,R=302]