Difficult problems with mod_rewrite - php

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]

Related

.htaccess Pretty URLS issue

Having a bit of a problem and wondering what I've done wrong.
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.rentus.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ category.php?param=$1&param2=$2 [L,QSA]
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ city.php?param=$1&param2=$2 [L,QSA]
So it works with general pages like "/about/" and then category.php with query strings on category.php "/category/hats/".
When I try to load "/city/losangeles/" Its loading category.php file instead of city.php file. Any idea what I can do to make this work properly and use the php file I tell it too?
Your rules are the exact same, so it wont ever reach the second:
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ category.php?param=$1&param2=$2 [L,QSA]
RewriteRule ^([-a-z]+)\/([-a-z]+)/?$ city.php?param=$1&param2=$2 [L,QSA]
Try this (Be aware, your param will no longer contain category/city, and will now include the actual category or city):
RewriteRule ^category\/([-a-z]+)/?$ category.php?param=$1 [L,QSA]
RewriteRule ^city\/([-a-z]+)/?$ city.php?param=$1 [L,QSA]

How to hide everything after your webpage name with .htaccess

When I enter my site and when I browse other sites, I get a parameter more: etc .php?p=pagename&id=1
I would like to hide everything after .dk
I have tried:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?p=$1 [L,QSA]
RewriteRule ^/index.php$ http://www.madsweb.dk/ [R,NC,L]
But it doesn't seem to work.
Can anyone help me out?
RewriteRule ^/index.php$ http://www.madsweb.dk/ [R,NC,L]
Rewrites work the other way around. You don't rewrite what the user sees in his address bar but you rewrite a request to another request (like a redirect, but handled transparently on the server).
That's also the reason that what you try to do there is not possible.
You could use a frame instead if you like the 90s and don't care about usability.
But jokes aside, it's the best to build upon this rule, which is already good:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?p=$1 [L,QSA]
and get the additional information (id in this case) into the pattern. Example:
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&id=$2 [L,QSA]
or more general (but still only one parameter):
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&$2=$3 [L,QSA]
I would like to hide everything after .dk
This is impossible. mod_rewrite cannot remove the need to have unique URLs for each resource.
(And if you did manage to do it, then it would break linking, including bookmarking and search engines).
Heeej
try
$pathInfo = explode('/', trim($_SERVER['PATH_INFO'], '/'));
$pagina = $pathInfo[0]; $actie = $pathInfo[1]; RewriteRule
^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA] RewriteRule
^([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&id=$2 [L,QSA]
or more general (but still only one parameter):
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([0-9]+)$ index.php?p=$1&$2=$3 [L,QSA]
or
list($pagina, $actie) = explode('/', trim($_SERVER['PATH_INFO'], '/'));
you also can try :
Options +FollowSymLinks RewriteEngine On
RewriteRule ^folder1/(.*)$
/Link.
com/folder2/$1 [R=301,L]

mod rewrite with get requests

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]

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.

Quick ModRewrite and GET question

I'm trying to return a success notice if the $_GET['success'] is set. It works fine if I input it like this: mail?inbox&success
But I'm trying to make it work if I input it like this: mail/inbox/success or mail/inbox&success
I'm not very experienced with mod_rewrite and I havent found a solution yet so I'm asking here. How would I get it to work with the examples above? This is what the .htaccess looks like right now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^user/([^/]*)$ /userinfo?name=$1 [L]
RewriteRule ^items/([^/]*)$ /items?weapon=$1 [L]
RewriteRule ^mail/inbox /mail?inbox [L]
RewriteRule mail/(.*)/(.*)$ /mail?read=$2
You could change
RewriteRule ^mail/inbox/ /mail?inbox [L]
to
RewriteRule ^mail/inbox/(.*) /mail?inbox&$1 [L]
Depends on what other combinations you want to rewrite.
Adding the line
RewriteRule ^mail/inbox/success /mail?inbox&success [L]
before the first rule should do it also, but only for this one case and may influence other rules.

Categories