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!
Related
I've been modifying my website URLs in order to beautify them a bit, but I'm currently having an issue regarding to the site directories.
For instance, my links look like users/[user_id]/ instead of users.php?p=[user_id]. Everything is working well apart of the directories, as said above.
When I try to access, for instance, to www.mywebsite.com/js/, I'm facing an error 500 from the server, which, hypothetically, means that there is no file/directory with this name, because of the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d line.
Here is the code I've put in my .htaccessfile:
RewriteEngine On
RewriteRule ^users/([0-9]+)/?$ /user.php?p=$1
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/?$1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# Error Documents Redirect
ErrorDocument 404 http://www.mywebsite.com/notFound/
ErrorDocument 403 http://www.mywebsite.com/forbidden/
ErrorDocument 401 http://www.mywebsite.com/unauthorized/
# Options
Options All -Indexes
Options All +FollowSymLinks
I don't really understand, as I just started working on this for the first time like a few hours ago, and I just start to understand the regex, and path formatting.
I hope you'll be able to help me, and through this, help people facing this issue too.
Regards
add this
RewriteCond %{REQUEST_URI} !^/(css|js)/
below
RewriteRule ^(.*)$ http://www.mywebsite.com/?$1 [L,R=301,NC]
please put this and check
and this after
RewriteEngine On
// having this rule in place we can remove the upper mentioned rule as apache will stop processing other conditions when see this rule
RewriteRule ^(css|jss)($|/) - [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've been assigned to a new website and I need to incorporate page routing and "vanity URLs" with .htaccess
I'm really not familiar with htaccess, and despite all my research efforts, I have not gotten any solid answers.
The website has a custom-built PHP admin system located in a subdirectory called "admin". I don't want to mess with it at all, so whatever changes I made to htaccess cannot affect that subdirectory.
So, given that the page has 3 main "pages": home, buy, and sell, I need requests made to "www.sitename.com" and "www.sitename.com/index.php" to route to the homepage. I need requests made to "www.sitename.com/sell" to route to the sell page.
And then the hardest part, I need requests made to "www.sitename.com/buy" to route to "buy.php", but requests made to "www.sitename.com/buy/category-name" to route to "www.sitename.com/buy.php?c=12"
I know its a lot to ask, but if anyone can guide me in the right direction, it would be much appreciated.
These rules should work for you:
RewriteEngine On
RewriteBase /
# skip admin OR any valid file/dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} /admin/ [NC]
RewriteRule ^ - [L]
RewriteRule ^(sell|buy)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(buy)/([^/]+)/?$ $1.php?c=$2 [L,NC,QSA]
Try this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^sell$ ./sell.php
RewriteRule ^buy$ ./buy.php
RewriteRule ^buy/(.*)$ ./buy.php?c=$1
If the category is always numeric replace the (.*) with (\d+)
I think this hope
/* copy and paste below, save the name. htaccess */
RewriteEngine On
RewriteBase /yousite.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
#ex1:http://yousite.com/buy/3
RewriteRule ([a-zA-Z_-]+)/([0-9]?) buy.php?c=$1
/* and in your file php */
<!-- #ex1:http://yousite.com/buy/3 -->
buy/3
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]