Regular expression for Url rewriting php - 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]

Related

How to Get Parameters from URL using .htaccess in PHP

How can I show following URL using .htaccess re-write rule with a non ? parameter?
Original URL
http://example.com/index.php?store=1
Desired URL
http://example.com/1/
OR
Desired URL
http://example.com/store/1
I am trying following code in .htaccess. What I am missing here?
RewriteEngine On
Options +Followsymlinks
RewriteCond %{REQUEST_FILENAME} !-f
rewriteRule ^store/(.+)\$ index.php?store=$1 [L]
my index.php file has this code
<?php
$storeid = $_GET['store'];
echo $storeid;
?>
What I am missing here?
You are escaping your end-of-string character, causing the expression to look for a literal $ sign so it never matches.
You need something like:
rewriteRule ^store/(.+)$ /index.php?store=$1 [L]
^ no back-slash here
or if you want to make sure you only match numbers:
rewriteRule ^store/(\d+)$ /index.php?store=$1 [L]

why this .htaccess regular expression is not working?

i have a regular expression as below
http://www.abc.com/signup.php?id=2
RewriteRule ^signup/([a-z]+)/([a-z]+)$ /signup.php?$1=$2 [L]
but this is not working? can any one help me.is there any thing to enable in the .htaccess file?
As you are passing also numbers, you should match them in regular expression as well:
RewriteRule ^signup/([a-z0-9]+)/([a-z0-9]+)$ /signup.php?$1=$2 [L]
You might want to include other chars as well or rather use metaclass such as \w.
After edits, try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^signup\.php$ /signup/%1 [L,R=301]
RewriteRule ^signup/([0-9]+)$ /signup.php?id=$1 [L]
That'll redirect /signup.php?id=123 to /signup/123 which will then be rewritten back to /signup.php?id=123 (but not redirected).

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

Url Rewriting regex

I have this rule in my .htaccess
RewriteRule ^([^/\.]+)/?$ ?page=user&id=$1 [L]
It rewrite a url like
http://sitename.ext/nickname
to
http://sitename.ext/?page=user&nickname
The problem is that with a url with dots like http://sitename.ext/nick.name.test i get a 404 error..
I'm not good with regex..
That's because it's not being rewritten. You specificaly told it to exclude ., and that's what it's doing.
Personally, I would favour something like this:
RewriteRule user/(.+) ?page=user&id=$1 [L]
If you want to match any character except a slash, the regex is [^/], since the \. will cause it also to not match dots.
Your rule should be
RewriteRule ^([^/]+)/?$ ?page=user&id=$1 [L]
You might find this site helpful.

PHP Mod_Rewrite

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]

Categories