.htaccess Pretty URLS issue - php

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]

Related

.htaccess filepaths differing how to solve

I've made www.site.com/asd to redirect to www.site.com/index.php?page=asd. However, I would like to add one more thing, where www.site.com/products/asd would redirect to www.site.com/index.php?page=products&item=asd
I can't seem to get it working, I have this as my base code:
RewriteEngine On
RewriteRule ^(admin|pictures)($|/) - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !=/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
I've tried adding:
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&item=$2 [L]
in various locations of the .htaccess file, which either results in:
500 Internal Server Error
or
0 CSS files load in .com/products/asd And no pictures load in .com/anyfile
I understand it's path related, but is this a good way to go about it? Can I solve it somehow in the .htaccess file, or would I need to add some check that if it's product page and a product is shown, include ../css?
The ^(.+)$ regular expression gives you a "500 Internal Server Error" because the redirection matches to the rule. So you gets the following error:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
You can use:
RewriteRule ^products/(.+)$ index.php?page=products&item=$1 [L,QSA]
RewriteRule ^([^\.]+) index.php?page=$1 [L,QSA]
So, the following URLs will be redirected to:
/products/asd to index.php?page=products&item=asd
/asd to index.php?page=asc
Change the last two lines like this :
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&item=$2 [L]
You should specify that in first rule a URI must come like /whatever/ or /whatever so the second will match either /whatever/whetever or /whatever/whatever/ and both will work individually without any interference.

Apache - .htaccess Rewrite Rules, unable to see the existing files content

I am developing an IFSC code finder script.
I have some files already named about.php, privacy-policy.php, contact-us.php, and some other files and folders.
My .htaccess file content is:
RewriteEngine on
RewriteRule ^index.html$ index.php [L]
RewriteRule ^about.html$ about.php [L]
RewriteRule ^privacy-policy.html$ privacy-policy.php [L]
RewriteRule ^contact-us.html$ contact-us.php [L]
RewriteRule ^search.html$ search.php [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/?$ index.php?bank=$1&state=$2&district=$3&branch=$4 [QSA]
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?bank=$1&state=$2&district=$3 [QSA]
RewriteRule ^(.*)/(.*)/?$ index.php?bank=$1&state=$2 [QSA]
RewriteRule ^(.*)/?$ index.php?bank=$1 [QSA]
Now, I want that the files which are already existing like about.html or about.php and so on should be shown instead of passing the query to custom rewrite condition. For example when I try to visit http://example.com/ifsc/about.html then the about.html file should be shown instead of passing it as http://example.com/ifsc/?branch=index.html
Thanks :)
Taking inspiration from this other answer, you should probably check beforehand if the requested file (not folder) exists, then skip all rewriterules if it does.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [PT,L]
(I'm not a guru in mod_rewrite, please double-check)

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]

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