htaccess specific queries rewrite - php

So I've got my htaccess to currently take anything after / and take it as a query.
So http://www.website.com/bacon is really http://www.website.com/index.php?type=bacon
The query is used to generate the type of content for the page. (A div contains different information based on the query)
However the query can only be of 3 different types. SO I have a problem where is a user were to go to http://www.website.com/baconandcheese then the DIV would be empty and look awkward.
So essentially I only want those 3 specific queries to be accepted, everything else would need to redirect to a 404 page.
RewriteRule ^([^\.]+)$ index.php?type=$1 [L]

You can have your rule like this:
RewriteEngine On
RewriteRule ^(bacon|cheese|ham)/?$ index.php?type=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . - [L,R=404]

This code should do it
#Only your three types
RewriteRule ^bacon$ http://www.website.com/index.php?type=bacon [nc]
RewriteRule ^type2$ http://www.website.com/index.php?type=type2 [nc]
RewriteRule ^type3$ http://www.website.com/index.php?type=type3 [nc]
#Pass files and folders
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#Throw 404
RewriteRule ^([^\.]+)$ - [L,R=404]
But you could also simply send a 404 with PHP:
header('HTTP/1.1 404 Not Found');
exit();

Related

Post and Portfolio Slug

I'm having this issue with using slug on portfolio page for a website being developed. Let me explain. I have a blog post that display the url as slug using htaccess. This is working. But now trying to use same for portfolio page coming from the database and it gives me no page found.
Below is the htaccess file
Options +MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-l [NC]
RewriteRule ^(.*)$ single.php?id=$1 [QSA,L]
#RewriteRule ^(.*)$ portfolio-single.php?page=$1 [L]
ErrorDocument 404 /404-error.php
And below is the portfolio-single.php page code
if(empty($_GET['page'])) {
Url::Redirect("/");
} else {
if(isset($_GET['page'])) {
$portfolio = Post::getPostById($conn, $_GET['page']);
}
}
Please I need help in resolving this issue.
Thank you.
If you want different requests to be rewritten to different files, you need to tell the rewrite modules what requests should go where. This can be done by just adding a prefix to one of them.
Options +MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-l [NC]
// Start with the portfolio
RewriteRule ^portfolio/(.*)$ portfolio-single.php?page=$1 [QSA,L]
// And have the "catch all" last (so it only catches requests that doesn't match
// any of the more specific rules.
RewriteRule ^(.*)$ single.php?id=$1 [QSA,L]
ErrorDocument 404 /404-error.php
The above will now make all requests that starts with /portfolio to be rewritten to the portfolio page while all the other requests will be handled by the single page.
This however means that you need to make sure all URL's to the portfolio starts with /portfolio (or what ever slug you want to use).

.htaccess code showing 500 internal error

Hi there I'm trying to make a blog demo and I'm having some pretty URL codes already.
i am having a url www.xyz.com
and a search url www.xyz.com/search/this+is+a+search+text
in search url the parameter search is a page name and this+is+a+search+text is a parameter that i'll be parsing
I'm having a .htaccess code below already
# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)$ app/post.php?category=$2 [L]
I'm using the below code for serach page
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php&searchstring=$2&page=$3 [L,QSA]
But while using the code I get a 500 internal error message, I'm not able to figure out what's the error!
I would really appreciate if anyone could help me out with this logic.
Rewrites in achieve /category/slug and for search page are the same. Search request matched with first - achieve rewrite and run app/post.php...
To answer your followup question to JarekBaran
JarekBaran: Rewrites in achieve /category/slug and for search page are the same.
What this means is that the RewriteCond are the same for the pretty URL code for the caterogy/slug page and the search page.
This means that the first match will always be used and so:
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
will always be triggered before
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
This way you will not be able to use the search function. You could consider adding a parameter to identify if a search is made or not.
Your problem is that you are starting your rules with (.+) which will match one or more of anything. This means that you are also matching the / character. Some of your 2 parameter redirects are matching your urls with 3 parameters because of this. You would be better of starting with something like this - ([a-zA-Z0-9-]+)
Your search rewrite should probably be something like this - RewriteRule ^/?([a-zA-Z0-9-]+)/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
I tested that rewrite on this htaccess testing tool and it is working.
That said your other rules will need to be changed as well since they all start with (.+). After doing that your category rewrite will conflict with your search rewrite.
However, this would probably work for you:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9-]+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)$ app/post.php?category=$2 [L]

How to reach 404 page through multiple htaccess rewrites

I'm pretty novice at htaccess but I know enough to make a few simple redirects and rewrites happen. My problem is that I've never combined multiple rewrites, such as in the custom PHP/MySQL gallery system I've created, and so I'm having a few issues.
The main things I want to accomplish are to have the gallery system structured as such:
https://example.com/album
https://example.com/gallery
https://example.com/gallery/img/img-name
This is actually working successfully already, but it seems to also be catching any invalid URLs and assuming they're gallery names, so my 404 page goes completely overlooked. So for example,
https://example.com/asdf
brings up a blank gallery page when it should obviously produce the 404. Do I need to somehow have my htaccess file check my database for potential existing name conflicts? Is that possible? Or is there a simpler solution that I'm overlooking?
Here's what I have now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /album.php?a_id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /galleries.php?g_id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z-]+)/img/([a-z0-9-]+)/?$ img.php?g_id=$1&i_id=$2 [NC]
Options -Indexes
ErrorDocument 404 /404.php
Thank you in advance!

How to make dynamic directories and subdirectories via .htaccess

I am trying to create an htaccess script that will create directories for either zero, one, or two variables.
My file that is being used accepts the following 2 get variables. make and model.
This is a 3 step page. I currently have the page located at at /new.php. This page allows a user to select the first variable (in my case, a vehicle make). By selecting a make on this page the user is taken to /new.php?make=Acura. This page now displays a list of all Acura models. From here, the user can click a model, and they will be directed to /new.php?make=Acura&model=TLX. They can now choose a submodel and will be taken to an information page.
So I'm trying to get:
new.php to go to /new/
new.php?make=XMake to go to /new/XMake/
and new.php?make=XMake&model=XModel to go to /new/XMake/XModel/
This is as far as I have gotten with my code:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^new/(.*)$ new.php?make=$1 [L,NC]
However any variables I add after this seem to break the first directory? Why is this?
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^new/([\w-]+)/([\w-]+)/?$ new.php?make=$1&model=$2 [L,NC,QSA]
RewriteRule ^new/([\w-]+)/?$ new.php?make=$1 [L,NC,QSA]
RewriteRule ^new/?$ new.php [L,NC]
The order of the rules is important. With this rule at the beginning, any request /new/, /new/XMake or /new/XMake/XModel/ matches, and the following rules are ignored.
To match the other rules, the more specific must come first, e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+?)/(.+)$ new.php?make=$1&model=$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+)$ new.php?make=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/$ new.php [L,NC]

Page not redirecting if not exists, just shows code with no data from other page

So I have an issue where I'm not being redirected to the 404 if the url doesn't actually exist, instead, it shows code from another page.
So I have a page called viewpost.php which typically works only if like viewpost.php?id=slug-text-of-post, but if you were to say go to bloggg.php, which doesn't exist, it would show you the viewpost page code as if you viewed it without any GET variables. So, the URL will show bloggg.php, but show the code from viewpost.php as if nothing would have been passed to it and shows this
empty page with no data to fill it
This below is my .htaccess
RewriteEngine On
ErrorDocument 404 http://www.example.com/404.php
RewriteBase /
RewriteRule ^c-(.*)$ viewcat.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Best way to achive what you want:
RewriteEngine On
ErrorDocument 404 http://www.example.com/404.php
RewriteBase /
RewriteRule ^cart/([0-9]*)$ viewcat.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^post/([0-9]*)$ viewpost.php?id=$1 [QSA,L]

Categories