Mod Rewrite Rules Confusion - apache

I am trying to do a few rules on my website, a few in particular
xxxx.com/administration/
xxxx.com/administration/users/
xxxx.com/administration/devices/
xxxx.com/administration/payments/
I'm trying to get it so on the administration page there is some text and links to the other 3 pages but any of the rules I try they just keep showing the same page even though the URL actually changes, so I assume the rules aren't being followed
I am currently trying the following - I have read up a bit on the rules but I will state here I am not an expert on them so please remember that.
RewriteRule administration/ administration.php
RewriteRule administration/users/ administrationusers.php
RewriteRule administration/devices/ administrationdevices.php
RewriteRule administration/payments/ administrationpayments.php
I have also tried the following sort of rule
RewriteRule administration/ adminfiles/administration.php
RewriteRule administration/(.*)/ adminfiles/$1.php
RewriteRule administration/(.*) adminfiles/$1.php
Any help would be appreciated
Thanks

This is a basic .htaccess file you should use.
RewriteEngine On
RewriteBase /
RewriteRule ^administration/?$ adminfiles/administration.php [L]
RewriteRule ^administration/([\w-]+)/?$ adminfiles/administration$1.php [L]
Note you website must be on the root folder (eg. for Wamp : wamp/www/).
If it's in a subfolder, use RewriteBase /myfolder/ (eg. for Wamp : wamp/www/myfolder/)

Related

RewriteRule 301 redirect for whole directory and sub-directories

I really need your help with this one...
I'm simply trying to redirect EVERYTHING in a directory to another. It looks simple when I read about it, but in real life, it's not working... Here is my entire .htaccess file right now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Redirect all to HTTPS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.org/$1 [R]
# End redirect
#301 REDIRECTS
Options +FollowSymLinks
RewriteRule ^mydir/(.*)$ /mydir-and-more/$1 [R=301,NC,L]
Fisrt, there is Wordpress stuff in didn't mess with.
Then, the code i copy/pasted from some site to redirect http to https. It works well. Note that i removed the "L" argument from the list to make sure my next rules will work.
After comes the part I'm strugling with.
So, it really is like that. My new directory starts with the same word then my old directory.
I copied this line from there: https://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
On the Apache web site (https://httpd.apache.org/docs/current/mod/mod_rewrite.html) it says that i should use a / between ^ and mydir. Tried it, didn't work.
I tried moving Options +FollowSymLinks at the top of the file. Nothing.
When i use something like this:
RedirectMatch 301 ^/mydir/ https://example.org/mydir-and-more/
This works. But only moves the exact /mydir/ address. It doesn't move the whole directory. Also, if I type in https://example.org/mydir without the last /, it won't work. If i add the / in the Redirect match, it doesn't work anymore because its the same word!
So, here I am, totally confused! Please, any expert advise on this one? Thanks!!
You need to move you rules before the wordpress defined ones.
In fact if you try to access you site the rewrite rules elaboration stops at
RewriteRule . /index.php [L]
That instruction means "manage all paths that are not already beign managed by upper rule and stop elaboration ([L] stands for last)".
You can safely place your rules above the wordpress ones, better in a ifmodule
<IfModule mod_rewrite.c>
RewriteRule ^/mydir/(.*)$ /mydir-and-more/$1 [R=301,NC,L]
</IfModule>
# BEGIN WordPress
Funniest thing is, I solved my problem by fooling around! I didn't really need the RewriteRule, all I needed to write instead of the RewriteRule was exactly this :
Redirect 301 /mydir https://example.org/mydir-and-more
I don't even need the Options +FollowSymLinks.

htaccess seo friendly urls, mod rewrite, redirection

I'm currently trying to make SEO friendly URL's for very specific URL's, not all of them. I've been at this for 48 hours with no luck. My goal is to make http://mydomain.com/index.php?p=g&id=1 look like this; http://mydomain.com/pageone/ - so far I have been able to achieve the redirection thusfar, but it is showing a 404 "The requested URL /pageone/ was not found on this server". My question is how to make it redirect to the virtual directory and not throw a 404.
Please note I want to add a rule for each page id, not a rule that changes everything from index.php?p=g&id=, just each specific link.
Below is my htaccess code:
Options +FollowSymLinks
Options -Multiviews
RewriteOptions MaxRedirects=1
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=g&id=1$ [NC]
RewriteRule ^index\.php$ /pageone/? [r=301,nc]
Any help with this would be GREATLY appreciated.
You redirected the index.php to /pageone/ but did not define what /pageone/ will actually show. You should add the following line to your .htaccess:
RewriteRule ^pageone/?$ index.php?p=g&id=1 [L]
and remove your rule. If you want to keep both RewriteRules, then add the following lines right after RewriteEngine On:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

ReWrite Rules Issue

I seem to be having an issue with my Apache Rewrites
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/?$ / [NC,L,R=301]
RewriteRule ^/$ wordpress/ [NC,L]
I simply need to remove /wordpress from the URL as I have pages within Wordpress I want to be seen as the main directory
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
Any help?
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/(.*)$ blog/$1 [L]
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
So it looks like you want to redirect the browser if someone makes a request for domain.com/wordpress/ to a URL without the wordpress bit, then internally rewrite the wordpress bit back into the URI? That's definitely do-able but if you have wordpress rewrite rules somewhere they're not going to play nicely with each other at all.
Any rules in the /wordpress directory will supercede any rules you put in the document root, which is where these rules need to go, and your remove-the-wordress-from-URI rules will be completely ignored. Even if you have rule inheritance turned on, the rules in the /wordpress directory will get executed first.
If all of your wordpress rules are actually in the document root's htaccess file, then just make sure to put these before the wordpress ones:
RewriteEngine on
RewriteBase /
# redirect the browser if someone makes a request for domain.com/wordpress/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /wordpress/
RewriteRule ^/?wordpress/(.*)$ /$1 [L,R=301]
# internally rewrite the wordpress bit back into the URI
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -f [OR]
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -d
RewriteRule ^(.*)$ /wordpress/$1 [L]

Apache URL Rewriting for multiple dynamic folder levels

Wondering if somebody can help me write some RewriteRule's for my website.
Take a look at the following URLs and see how I need to rewrite them.
http://www.example.com/essays-and-reports/
does not need to be re-written, it is a physical folder on the web server.
http://www.example.com/essays-and-reports/business/
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/dynamic_name2
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/business/financial-reports/
needs to rewrite to (root)/second_level_template.php
http://www.example.com/essays-and-reports/blah/financial-reports/C_B_2413_Report_on_savings.php
needs to rewrite to (root)/final_level_template.php
Note the rules must work regardless of a trailing slash. To sum-up, there are three levels which I need to re-write to their relevant template. None of above exists physically on the server including the PHP file for final level. The only thing that exists is the essays-and-reports folder which is main folder for the website.
I tried something like this but I get compile errors in the log.
RewriteRule ^([^/]+)/([^/]+)/?$ /second-level-template\.php [L]
If you could help me write the rules I need - I appreciate it greatly.
EDIT:
This code kind of works but it also rewrites the essays-and-reports folder which I don't want...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /essays-and-dissertations/
RewriteRule ^[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
RewriteRule ^[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^[^/]+/?$ /first_level_template.php [L]
You could do this with 3 rules:
RewriteRule ^essays-and-reports/[^/]+/?$ /first_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
You don't need to escape the dot in your redirect target, since that's not a regular expression.

preventing direct download of SQLite file using .htaccess

I know the basics of .htaccess, but freely admit to being a non-expert when it comes to mod_rewrite.
The problem:
I have a sqlite db file in the document root (no, I can't move it out of here) called data.sqlite. This file is accessed internally by PHP files, however, I want to protect this file from being 'downloaded' by the user typing the db URL directly into their browser.
I have used .htaccess to create pretty URLs in the past, and thought using mod_rewrite would provide a nice solution to my problem. However, my rewrite rule does not seem to prevent access.
.htaccess
Options +FollowSymLinks
Options -multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC]
##prevent access to any file data.sqlite by redirecing back to index.php
RewriteRule ^data.sqlite$ index.php [L,NC]
##my other rules follow - not shown here
Any ideas where I'm going wrong with the rewrite?? I'm sure it is something simple?
EDIT:
Ideally, I'd like to prevent direct URL access to all files ending in .sqlite, not just data.sqlite
Here's how you do it because I have also done it:
RewriteCond %{HTTP_REFERER} !^http://*.webwarecollection.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://*.webwarecollection.com$ [NC]
RewriteRule .*\.(sqlite)$ - [F,NC]
About "start with ." that is not true!. File must start on ".ht" to be blocked by default on appache.
httpd disables access to files that start with a . by default. Rename your file to .data.sqlite and it will be dealt with.