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).
Related
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!
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]
I am writing a website which is dynamically populated through an Oracle database.
I have completed the desktop site and am now required to create a mobile site. Due to how different the sites are planned to look, I have opted to create 2 different "template" like websites for the mobile and desktop sites.
However, for my desktop site, everything is built off the index.php file in order to allow it to be completely dynamic. Pages are therefore look like www.domain.com/index.php/page in the url.
For the desktop site, this works. I am using a generic index.php removal rewrite rule in order to then make the url www.domain.com/page however still display the same page as the previous URL.
My issue, is that now I have a www.domain.com/mobile/index.php. Which has been created and for the most part has been working, however when trying to add addition dynamic pages to the mobile site. www.domain.com/mobile/index.php/about for example just redirects to www.domain.com/mobile/ and it doesn't even include the about part of the URL.
After much debugging, I have discovered it is definitely the .htaccess that is causing the issue.
If you have any insight into my issue, please help me out.
Thanks in advance
EDIT 1
Rewrite Rules are as follows
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this code in your /mobile/.htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /mobile/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ $1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
This will override all the rules present in parent .htaccess for /mobile/ URI path.
Simplified version to make it work in root .htaccess:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(mobile)/(.*)$ $1/index.php/$2 [L,NC]
# Directs all EE web requests through the site index file
RewriteRule ^(.*)$ /index.php/$1 [L]
Based on the debugging you mentioned, there is a rule in your .htaccess which is rewriting www.domain.com/mobile/index.php/about to www.domain.com/mobile/. So, if you find which rule this is, you can add one above it that will catch requested URLs for your mobile pages and then not allow the problematic following rule to run. Something like this:
RewriteRule ^mobile/index.php/([a-zA-Z0-9-_]+)$ ^mobile/index.php/$1 [L,QSA]
The L ensures that if the user's request matches this rule, no further rules (including the one causing the issue) will be executed.
Thank you for the answers you've both given, however neither of them worked, I've now solved the issue, it was to do with the final RewriteRule at the end
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I needed to change it to
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/mobile/.* [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /mobile/.* [NC]
RewriteRule ^(.*)$ mobile/index.php/$1 [L]
So that it would work with both mobile and desktop sites.
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();
I have the following HTACCESS code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_URI} !(/cms/|/js/|/mobile/)* [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /page.php [L]
As it stands, it works as I need it to within the root of the site, but accessing an admin page (/cms/), I can tell via PhpConsole that it is still hitting page.php. I think this is because the admin is controlled with a query string: URIs look like /cms/?view=pages&action=edit&id=4
If I uncomment the first condition, this problem no longer occurs, but my front end comes back with a 404 on files that don't exist, rather than redirecting to page.php
What is wrong with my code?
Change your code with this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|js|mobile)(/.*|)$)^.*$ page.php [L,NC]