mod rewrite with get requests - php

I am having issue passing get variables.
index?p=calendar refers to calendar.php located in pages/calendar.php and index.php is in root.
my URL is localhost/researchportal/calendar/11/2011
Calendar has 2 get variables, month and year. i.e calendar.php?month=11&year=11
here is my rule, but its not working.
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]
I also tried
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
.htaccess file
RewriteEngine On
RewriteBase /researchportal/
RewriteRule ^/calendar/([0-9]+)$ index.php?p=calendar [QSA,L]
RewriteRule ^users/login /researchportal/pages/login.php [L]
RewriteRule ^users/logout /researchportal/pages/logout.php [L]
RewriteRule ^users/register logout.php [L]
RewriteRule ^profile/([0-9]+)$ index.php?p=profile&usr_id=$1 [QSA,L]
RewriteRule ^profile/edit/([0-9]+)$ index.php?p=edit&usr_id=$1 [L]
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?p=$1 [L]

^calendar/([0-9]+)$/([0-9]+)$
Why are you ending ( $ ) it twice?
^ starts and expression and $ ends it. There is no reason to have neither in the middle of an expression.
RewriteRule ^calendar/([0-9]+)/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
Should work better. If you for some reason would also want some query variables in the url, you can replace [L] with [L,QSA] aswell.

I think the problem is because you have an additional $ character.
RewriteRule ^calendar/([0-9]+) *$* /([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]

Related

Difficult problems with mod_rewrite

I have a difficult problem:
In my .htaccess I have the following RewriteRules which do not function.
RewriteRule u/(.*)/ user.php?u=$1 [L,QSA]
RewriteRule u/(.*) user.php?u=$1 [L,QSA]
RewriteRule user/(.*)/ user.php?u=$1 [L,QSA]
RewriteRule user/(.*) user.php?u=$1 [L,QSA]
RewriteRule view/(.*)/ view.php?file=$1 [L,QSA]
RewriteRule view/(.*) view.php?file=$1 [L,QSA]
RewriteRule show/(.*)/ show.php?img=$1 [L,QSA]
RewriteRule show/(.*) show.php?img=$1 [L,QSA]
RewriteRule report/(.*) report.php?img=$1 [L,QSA]
RewriteRule report/(.*)/ report.php?img=$1 [L,QSA]
RewriteRule search/tag/(.*) search.php?t=$1 [L,QSA]
RewriteRule search/tag/(.*)/ search.php?t=$1 [L,QSA]
RewriteRule search/(.*) search.php?q=$1 [L,QSA]
RewriteRule search/(.*)/ search.php?q=$1 [L,QSA]
RewriteRule bug/view/(.*)/ bug.php?view=$1 [L,QSA]
RewriteRule bug/view/(.*) bug.php?view=$1 [L,QSA]
RewriteRule bug/(.*) bug.php?step=$1 [L,QSA]
RewriteRule bug/(.*)/ bug.php?step=$1 [L,QSA]
However, when I enter a RewriteRule as
RewriteRule ^ http://example.com [R, L]
I will be forwarded, so it works.
Solutions such as
RewriteRule u/(.*)/ http://example.com/user.php?u=$1 [L,QSA]
do not work. Also
RewriteRule /u/(.*)/ user.php?u=$1 [L,QSA]
or the like were not successful.
I have the problem since I recently moved to a new server. However, everything seems to be fine with the configuration.
Does anyone have an idea what I'm doing wrong?
Did you search for a possible answer?
I found this:
Why does this RewriteRule work with [R] but not with [QSA,L]?
It seems that:
The FastCGI version when paired with Apache 2.2 doesn't seem to like
the rewriterule index.php/$1. Instead it prefers index.php?$1 but some
CMS don't like that. The CMS I am using does not like that. So
undone's comment to use ?$1 was on the right track, but because I had
no explanation, and the CMS I am using (Open Journal Systems) does not
work with the pathinfo stuff after a ?, then it was just not working.
The solution was to change from FastCGI to just regular CGI.
check the other answers under that URL, they have valid points, that might help your case.
To me it seems like you are using a wrong syntax. Try to put your pattern in double quotes and see if it works (remember: the pattern in RewriteRule is a Regular Expression). Like this:
RewriteRule "u/(.*)/" "http://example.com/user.php?u=$1" [L,QSA]
Or a better way is to enclose your patterns in: ^ (as start of string) and $ (as end of string):
RewriteRule ^u/(.*)$ http://example.com/user.php?u=$1 [L,QSA]

.htaccess rewrite $_GET variables

I have the following page:
http://localhost/?news=19&page=2
I want to rewrite this so that it goes kind of as follows
http://localhost/news/19/page/2
This is my .htaccess file, but my code dont work :(
RewriteEngine ON
RewriteBase /
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(.*)$ /?news=$1 [L]
RewriteRule ^news/(.*)/page/(.*)$ /?news=$1&page=$2 [L]
You should specify more precicely what the allowed type of character is and you should make sure your previous rules don't cause your later rules to be ignored.
So you should put your most specific rule first and if you want digits for your news- and page ID's, you should use for example:
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(\d*)/page/(\d*)$ /?news=$1&page=$2 [L]
^^ just a digit instead of any character `.`
RewriteRule ^news/(.*)$ /?news=$1 [L]

Mod Rewrite and Arabic characters

I currently have the following rules in my .htaccess file that work perfectly for the English site.
RewriteEngine On
RewriteRule ^index.html$ index.php [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/?$ rewrite.php?param1=$1 [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ rewrite.php?param1=$1&param2=$2 [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2&param3=$3 [L]
But, on the Arabic website, this throws a "404" error.
Examples that work:
http://www.mysitedomain.xyz/work/website/pages/goals-and-objectives
http://www.mysitedomain.xyz/work/website/solutions/banking
Examples that DO NOT work:
http://www.mysitedomain.xyz/work/website/pages/الاهداف
http://www.mysitedomain.xyz/work/website/solutions/بنوك
Any idea how to fix that?
Thanks
Given your URL layouts, I don't think you really need to be so specific. You probably just want to split on slashes:
RewriteEngine On
RewriteRule ^index.html$ index.php [L]
RewriteRule ^/?([^/]+)/?$ rewrite.php?param1=$1 [L]
RewriteRule ^/?([^/]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2 [L]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2&param3=$3 [L]
in place of ([0-9a-zA-Z_-]+) this utilize (.*), for any content

htaccess problem in php

There are two kind of pages -
www.mysite.com/about.php
www.mysite.com/winners.php?y=2008
I want to make them like -
1. www.mysite.com/about
2. www.mysite.com/winners/2008
my htaccess is like -
RewriteEngine On
#removing .php ext(type-1)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for type-2 (with parameter)
RewriteRule winners/([0-9]+)/?$ winners.php?y=$1 [L]
RewriteRule winning-photograph/([0-9]+)/?$ winning-photograph.php?y=$1 [L]
RewriteRule award-giving/([0-9]+)/?$ award-giving.php?y=$1 [L]
RewriteRule album-launch/([0-9]+)/?$ album-launch.php?y=$1 [L]
RewriteRule district-fest/([0-9]+)/?$ district-fest.php?y=$1 [L]
RewriteRule lifetime-achievement-awards/([0-9]+)/?$ lifetime-achievement-awards.php?y=$1 [L]
RewriteRule critics-award-for-tv/([0-9]+)/?$ critics-award-for-tv.php?y=$1 [L]
RewriteRule photography-book/([0-9]+)/?$ photography-book.php?y=$1 [L]
The two section of this htaccess are not working at the same time. what can I do? also - is there a way to simplify the 2nd section?
Your first RewriteRule is basically preventing all the other rules from executing. It does that rule for both types of your URLs, then the [L] flag stops it from evaluating the other rules.
I would say put that rule at the very end. Do all the more-specific ones first, then have that as a generic one for all other cases, only to be evaluated if the rest of the rules didn't match the current URL.

"RewriteBase: argument is not a valid URL" error

I'm trying to configure .htaccess of my website.
http://213.175.210.49/~incisozl/ is the temporary url to the root(~/public_html/).
when I try to rewrite the url at .htaccess i get an
/home/incisozl/public_html/.htaccess: RewriteBase: argument is not a valid URL, referer: ht tp://213.175.210.49/~incisozl/inci-sozluk/somestring
error.
my rewrite rule is;
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /index.php [L]
RewriteRule ^inci-sozluk/([^\.\?/]+)/([0-9]+)/?$ /seo.php?process=word&q=$1&sayfa=$2 [L]
RewriteRule ^inci-sozluk/([^\.\?/]+)?$ /seo.php?process=word&q=$1 [L]
RewriteRule ^inci-sozluk/([^\.\?/]+)/([0-9]+)/([0-9]+)/?$ /seo.php?process=word&q=$1&sayfa=$2&gid=$3 [L]
RewriteRule ^inci-sozluktest/([^\.\?/]+)/([0-9]+)/([0-9]+)/?$ /seo.php?process=wordtest&q=$1&sayfa=$2&gid=$3 [L]
RewriteRule ^inci-sozluk-bugun/([^\.\?/]+)/([0-9]+)/?$ /seo.php?process=wordbg&q=$1&sayfa=$2 [L]
RewriteRule ^inci-sozluk-bugun/([^\.\?/]+)/([0-9]+)/([0-9]+)/?$ /seo.php?process=wordbg&q=$1&sayfa=$2&gid=$3 [L]
RewriteRule ^inci-sozluk-dun/([^\.\?/]+)/([0-9]+)/([0-9]+)/?$ /seo.php?process=worddn&q=$1&sayfa=$2&gid=$3 [L]
RewriteRule ^inci-sozluk-dun/([^\.\?/]+)/([0-9]+)/?$ /seo.php?process=worddn&q=$1&sayfa=$2 [L]
RewriteRule ^inci-sozluk-ters/([^\.\?/]+)/([0-9]+)/?$ /seo.php?process=wordts&q=$1&sayfa=$2 [L]
RewriteRule ^inci-sozluk-ters/([^\.\?/]+)/([0-9]+)/([0-9]+)/?$ /seo.php?process=wordts&q=$1&sayfa=$2&gid=$3 [L]
RewriteRule ^inci-sozluk-cvpters/([^\.\?/]+)/([0-9]+)/?$ /seo.php?process=cvpwordts&q=$1&sayfa=$2 [L]
RewriteRule ^inci-sozluk-cvpters/([^\.\?/]+)/([0-9]+)/([0-9]+)/?$ /seo.php?process=cvpwordts&q=$1&sayfa=$2&gid=$3 [L]
RewriteRule ^inci-sozluk-ileti/([0-9]+)/?$ /seo.php?process=eid&eid=$1 [L]
RewriteRule ^inci-sozluk-ileticvp/([0-9]+)/?$ /seo.php?process=cvpeid&eid=$1 [L]
</IfModule>
btw. it works fine when i use it with www.incisozluk.org pointed domain
That's strange since the only place where this error message is generated is in
cmd_rewritebase() in modules/mappers/mod_rewrite.c and the code is
if (a1[0] != '/') {
return "RewriteBase: argument is not a valid URL";
}
It tests whether the first character of the new value for RewriteBase is a /.
According to the code you've posted that is the case. Could there be another .htaccess file that has an invalid value?
This is archaically old, but I just ran into this and for me the issue was having a slash on both the RewriteBase line and the RewriteRule line's destination. I took the slash off the destination and the error went away!

Categories