mod_rewrite code check double rewrite rule - php

Here is my current config:
1.RewriteRule ^india/news.php /portal/india/stories/telugu.php??topicid=14
2.RewriteRule ^india/telugu.php?currentpage=([0-9]+).&topicid=14 /portal/india/stories/telugu.php?currentpage=$1&topicid=14
Rule 1 rewrites http://www.domain.com/portal/india/stories/telugu.php?topicid=14 to
http://www.domain.com/india/news.php which works fine.
Rule 2 should rewrite http://www.domain.com/portal/india/stories/telugu.php?currentpage=p.no&topicid=14 to http://www.domain.com/india/telugu.php?currentpage=p.no&topicid=14 which doesn't work.
It shows a 404 not found.
Here p.no = numeric number
If I need to elaborate my problem, please let me know.

RewriteRule uses regular expressions so having a ? in the regular expressions is not denoting a query string but instead kicking off as a regular expressions key.
Also RewriteRule really only looks at the request URI and ignores the query string unless you set a conditional for the query string.
so Rule 2 should be something like this
RewriteCond %{QUERY_STRING} currentpage=([0-9]+).&topicid=14
RewriteRule ^india/telugu.php /portal/india/stories/telugu.php?currentpage=%1&topicid=14
Please note that in this setup you need %1 instead of $1
I hope this helps I have had this issue several times and this is the only way I ever get it to work. Also you can test rewrites before you do them on sites like http://martinmelin.se/rewrite-rule-tester/

Related

Regular expression to extract target url from a htaccess rewrite rule string in php?

I need to check our htaccess rewrite rules if the target url is still ok. For this task I want to be able extract the target part from any rewrite rule. Actually what I need is an appropriate regular expression pattern.
Typical htaccess rewrite rules may look like this:
RewriteRule ^media/content/some_source.pdf$ / [R=301,L]
RewriteRule ^another_source/with_subfolder([-0-9a-zA-Z\w]*) landingpages/some_folder/target.php [L]
The fat part needs to be extracted! Hope somebody is more familiar with regex to help me solve this issue.
Thanks
explode on ' ' space and then the 3rd item (index 2) will do the job just fine

Trouble with URL rewriting in .htaccess

My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1

How to convert plus (+) sign to "=+" with htaccess?

I want to convert every url which contains "+" to "=+"
for example that url:
http://www.bedavaemlaksitesi.com/mersinemlakrehberi210/index3.php?+
should be like this:
http://www.bedavaemlaksitesi.com/mersinemlakrehberi210/index3.php?=+
tried that and few other lines but doesn't work so far, i'm guessing it causes a loop or something.
RewriteRule ^([^/\.]+)+([^/\.]+)?$ $1=+$2 [R]
I'm just gonna give you a literal answer for that specific example. Not sure if that will actually help you:
RewriteCond %{QUERY_STRING} ^([+])$
RewriteRule /index3.php$ index3.php?=(%1) [R,L]
You cannot repleace each + in the QS, as you do need a separate condition to match it first.
Also about your original rule:
RewriteRule ^([^/\.]+)+([^/\.]+)?$ $1=+$2 [R]
Escaping the dot in the charclass is redundant, [^/.] suffices. And you need at least a separator between the two groups / to make sense. But you can't match the query_string there, that's handled separately from the current filepath.
See alsos: ServerFault: Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask? -and- HttpdWiki: Manipulating the Query String

Htaccess Rewrite Rule Conditional

I am a complete noob when it comes to the .htaccess file... What I'm trying to do is check to see if a GET variable exists, ?si=10, and pass that onto the rewrite, but only if it exists.
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1
This way when I have say website.com/user/Username/ it goes to, on the server, website.com/profile/?name=Username instead. But when I add do website.com/user/Account/?si=10, the server isn't passing the si variable onto the actually loaded page. Any idea how I would do this? Sorry if I worded this badly..
Try the QSA flag
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1 [QSA]
From the manual...
When the replacement URI contains a
query string, the default behavior of
RewriteRule is to discard the existing
query string, and replace it with the
newly generated one. Using the [QSA]
flag causes the query strings to be
combined.
You can do it like this:
RewriteCond %{QUERY_STRING} ^si=([0-9]+)$ // replace regex as neccesary
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1&si=%1
The percentage sign % brings in the match or matches from the rewriteCond(s) and you use the dollar sign as normal for the matches from the rewriteRule. This gives you full control of your query vars, but yes, QSA is simpler.

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