PHP Mod_Rewrite - php

I'm trying to use mod_rewrite to achieve the following rewrite:
From - http:// pre.domain.com/public/?project=Awesome
To - http:// pre.domain.com/project/Awesome
Can't seem to figure it out (despite reading through endless sites). Any help?

RewriteEngine On
RewriteRule ^project/(.*) /public/?$1 [L,NC]

RewriteEngine On
RewriteCond %{QUERY_STRING} ^project=(.*)$
RewriteRule ^/public/ /project/%1/? [R,L] # strip off query string
You need to match against the query string using a RewriteCond.

Not sure you have your URLs around the right way so I think your are trying to do this:
RewriteRule ^project/(Awesome) /public/?project=$1 [L]

Related

How to Make .htaccess search by allow mod_rewrite as query string and page number?

My original file is a PHP page named search.php.
Normally, the search function will be:
mydomain.com/search.php cid=&type=search&q=keyword+text&page=2
However, is it possible to rewrite it to mydomain.com/all.html?q=keyword+text&page=[1-anypage]?
I tried to put this code into .htaccess:
RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]
but something went wrong.
Please help me find the solution. Thanks.
Yes its possible but you can't match against query string in pattern of a RewriteRule, you will need to use RewriteCond directive to check %{QUERY_STRING} something like the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]

.htaccess PHP rewrite link

I have problem and dont have any idea how to solve it.
I have URL like this:
http://somedomain.com/link.php?lnk=1&sid=dsds
and need to change it to this:
http://somedomain.com/link/1/dsds
If there is only one variable I use this:
RewriteEngine on
RewriteRule ^link/([a-zA-Z0-9_-]+)$ link.php?lnk=$1
But I don't have any idea how to attach that second variable &sid={something}
Thanks in advance for any help.
RewriteEngine on
RewriteRule ^link/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ link.php?lnk=$1&sid=$2
if the first parameter/value uses digits only, then you can use
RewriteEngine On
RewriteRule ^link/([0-9]+)/([a-zA-Z0-9_-]+)$ link.php?lnk=$1&sid=$2 [L]
Apache mod_rewrite Introduction (see "Figure 1" in "Regex Back-Reference Availability" section)
RewriteRule Flags
Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lnk=(.+)\&sid=(.+)
RewriteRule ^link\.php link/%1/%2/? [R=301,L]

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]

mod rewrite, htaccess and gettign the values

the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies

Regular expression for Url rewriting php

I'm using URL rewriting with Wamp but I don't find the correct regex for my needs.
I'd like to transform http://localhost/site_artisans/site_artisans/peintre-annecy.php in http://localhost/site_artisans/site_artisanspeintre-annecy.php (remove the slash between site_artisans/site_artisans and whatever is after).
I thought of :
RewriteEngine on
RewriteRule .*site_artisans/site_artisans/.* site_artisans/site_artisans [L]
(Unknown number of characters before and after and slash removed).
But this doesn't work.
Try
RewriteEngine on
RewriteRule .*site_artisans/site_artisans/(.*) site_artisans/site_artisans$1 [L]
I tested it on http://martinmelin.se/rewrite-rule-tester/ and I believe the result is what you want.
Try this:
RewriteEngine on
RewriteRule .*site_artisans/(site_artisans/.*) site_artisans/$1 [L]

Categories