Quick ModRewrite and GET question - php

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.

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]

.htaccess RewriteRule for category/page in custom CMS

I have a working RewriteRule that rewrites any page to the literal url:
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
The problem is I want to add page=$1&category=$2 and convert it to...obviously... /category/page
I tried this, but it doesn't seem to work:
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
I should note that I would still like to be able to access pages without categories - ie /login or /about that should go to index.php?page=about for instance
Your solution will be as below.
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?page=$1&category=$2 [NC,L]
Input url : http://www.test.com/my-cat/my-page
and it will be treated as : http://www.test.com/index.php?page=my-cat&category=my-page
Try with below, we are instructing apache to don't look for directory or file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
try this
#https://www.example.com/index.php?category=9&page=CBSE `#https://www.example.com/category/page/CBSE`
Method One:
#use this code for generate new url
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2 [NC,L]
Method Two :
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2
Note :Hit Your Url index.php?category=$1&page=$2 to convert $i & $2 Create Dynamic Url Your Id Bases
This will work for you.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&page=$2 [L]
If you want to generate rewrite urls easily, there are a lot online rewrite url generators.
Thanks for all the contributions...
Because I needed to cater for both /page and /category/page...I ended up doing this:
RewriteRule ^([0-9a-zA-Z_\-][^/]+)$ index.php?page=$1 [N]
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?category=$1&page=$2 [L]
But that was only half the solution as I needed to modify my PHP to check whether category is set or not and to send a 404 if it doesn't match. Otherwise /invalid_category/valid_pagewould still work.
The ideas above all pushed me in the right direction, thanks.

Rewrite - 2 files with same parameters and located in same folder

I know that was a frequent question here in SO, but I can't rewrite some dynamics URL.
Hope someone can help me.
I've 2 files located in the same folder, that receive same number of parameters:
.com/serie.php?id=__123__&nombre=__string__
.com/episodio.php?idSerie=__123__&query=__1x1__
My desired pretty url are this:
for serie.php: .com/__123__/string
for episodio.php: .com/__123__/string/1x1-some-other-strings-not-relevants-here
I was able to make the first rewrite, like this:
Options +FollowSymLinks
RewriteEngine on
# Rewrite Serie.php
RewriteRule ^([^/]*)/$ /serie.php?id=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /serie.php?id=$1&nombre=$2 [L]
Work fine, but the problem comes in the second rewrite.
The bold characters are "relevants" and I use this, to make some work.
I tried something like this:
RewriteRule ^([^/]*)-([^/]*)$ /episodio.php?idSerie=$1&query=$2 [L]
But I get 404 not found.
How can make this second rewrite?
Thanks in advice.
You can use:
Options +FollowSymLinks
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ serie.php?id=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ serie.php?id=$1&nombre=$2 [L,QSA]
RewriteRule ^([^/]+)/[^/]+/([^-]+)-[^/]*/?$ serie.php?id=$1&query=$2 [L,QSA]

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 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.

Categories