rewrite url in .htacess not working in php - php

I am working using php and apache server, I want to rewrite sites/all/themes/chap2014/fpd/gift.php to fdp/gift and sites/all/themes/chap2014/fpd/another.php fdp/another
exactly I want do this
rewrite sites/all/themes/chap2014/fpd/*.php to fpd/*
rewrite mode is enable and I try below code in .htacess file.
RewriteEngine on
RewriteRule ^fpd/?$ sites/all/themes/chap2014/fpd/$1.php
but nothing happened,what is the correct way to achieve this ?
Appreciate any help,

You need to first group and capture part of URI on LHS of URI pattern before you can use $1, $2 etc on RHS.
RewriteEngine on
RewriteRule ^fpd/([^/]+)/?$ /sites/all/themes/chap2014/fpd/$1.php [L,NC]
PS: In your question you have used both fpd and fdp. Not sure which one is right.

Try this:
RewriteRule ^fpd/(.+)$ sites/all/themes/chap2014/fpd/$1.php
You might want to exclude the / symbol, if so, then replace the .+ with [^/]+.
You have to enclose the part that you want placed into a variable with (), and each enclosed part will be put into $1, $2, $3 and so on.
Also, here is a nice tool to test your rewrite rules:
http://martinmelin.se/rewrite-rule-tester/

Related

How to write redirection rule in .htaccess using Regex

can someone tell me how to write redirection rule using regex in .htaccess file.
I want
www.example.com/healthcare/practitioner/nikky.23
to be redirectd to
www.example.com/healthcare/practitioner/#/nikky.23
Note that last parameter after '/' i.e nikky.23 can change in different cases.
Please help.
Try with RewriteRule:
RewriteRule (healthcare/practitioner/).* $1
If you want the last parameter to be without further slashes:
RewriteRule (healthcare/practitioner/)[^/]*$ $1

Rewrite variable url in .htaccess from page.{variable}.php to /page/{variable}

I don't have much experience with .htaccess file, but I want to achieve the following:
/page/{variable} should be redirected to the file page.{variable}.php
I tried something like this:
RewriteEngine On
RewriteRule ^page/$ page.$.php`
But that didn't work. How can I rewrite this?
In the matching expression, $ signifies the end of the string.
What you want is a capturing group which you can refer to using $1 (for the first captured group):
RewriteRule ^page/(.+) page.$1.php

Using .htaccess for a variable folder

I am having a little trouble with using .htaccess for what I need it to do.
This is the URL of the webpage:
http://example.com/folder/$1/topic.php?id=$2
The only two things that change are $1 and $2.
I was using this code, but I cannot seem to get it to work with the variable folder.
RewriteRule ^([a-zA-Z0-9_-]+)$ topic.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ topic.php?id=$1
RewriteRule ^folder/$1/([a-zA-Z0-9_-]+)$ folder/$1/topic.php?id=$2
RewriteRule ^folder/$1/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2
I may have made some mistake with the syntax but I cannot seem to figure it out.
The browser URL will change to look like:
http://example.com/folder/$1/topic/$2
Thank you for your time ^.^
Yay! user-sama.
I think you need to escape $1, just give it some backslash ninja punch:
RewriteRule ^folder/\$1/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2
Or it might be:
RewriteRule ^folder/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ folder/$1/topic.php?id=$2
^__^
Yeah, i need to explain what's going on, so what's between parentheses in the left becomes $ on the right, the number that comes after $ is the present order of parentheses. So:
# 1st 2nd 3rd 1st────┐ 2nd ┌──────3rd
RewriteRule ^(.*?)/(.*?)/(.*)$ file.php?p1=$1&p2=$2&p3=$3
These parentheses blocks are called capture block, it's RegExp.

URL Rewrite with htaccess and PHP

I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.

Making wordpress like url by mod-rewrite

I am trying to edit the URL http://example.com/paper-ads-details.php?req=43674&rnd=1308632400 into http://example.com/ads/43674/1308632400 by following rewrite_rule
RewriteRule ^ads/([0-9]*)/([0-9]*)$ paper-ads-details.php?ads&req=$1&rnd=$2
But it's not working. I want to just hide the parameters. Any other suggestion will be appreciated. Thanks.
Both your rewrite rules make no sense. There are no $1 or $2 and what you are trying to do is impossible as no webserver can guess what the req and rnd parameters were.
You may be interested in rewriting http://example.com/ads/43674/1308632400 into http://example.com/paper-ads-details.php?req=43674&rnd=1308632400. Here is the code:
RewriteEngine On
RewriteRule ^ads/([0-9]*)/([0-9]*)$ paper-ads-details.php?req=$1&rnd=$2
If you want to rewrite: http://example.com/paper-ads-details.php?req=43674&rnd=1308632400 into http://example.com/ads?req=43674&rnd=1308632400, then try this:
RewriteCond %{QUERY_STRING} ^req
RewriteRule paper-ads-details.php?(.*)$ http://example.com/ads?$1 [L]
You can validate your rewrite-rule here - http://martinmelin.se/rewrite-rule-tester/
The question is quite confusing, made more so by the sample you provided, but if I understand correctly that you want the URL to contain no more parameters, then you won't have any $1 or $2 to work with. These would be the parameters that you match with your regular expression, but your regular expression doesn't match any parameter.
So, if you want /ads to go to /paper-ads-details.php?req=43674&rnd=1308632400, you can simply write a rule that does exactly that:
RewriteRule ^ads$ /paper-ads-details.php?req=43674&rnd=1308632400 [L]
The above rule will match the URL /ads and will call /paper-ads-details.php?req=43674&rnd=1308632400 every time that URL is requested.

Categories