htaccess mod_rewrite internal server error - .htaccess

So I'm trying to clean up my URLs ready for my sites public release, this is how current URLs look like:
http://pattersoncode.ca/index.php?a=help
But I'm trying to turn that into
http://pattersoncode.ca/help
I used a mod_rewrite generator online, and it gave me this:
RewriteEngine On
RewriteRule ^([^/]*)$ /?a=$1 [L]
But that gives me an Internal Server Error. Any suggestions?

Your rules are looping, since ^([^/]*)$ will match index.php (or a blank URI). The rewrite engine will keep applying all the rules until the URI stops changing. You can add some conditions to prevent this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /?a=$1 [L]
But are you sure mod_rewrite is loaded? If you put these tags around the rewrite stuff:
<IfModule mod_rewrite.c>
</IfModule>
Are you still getting the 500 server error? If not, then you need to make sure you're loading mod_rewrite in the httpd.conf file, and restart apache.

Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?a=$1 [L]
It rewrites any URL except a real file or directory to index.php?a=xyz.

Related

How to remove index.php using htaccess

I am currently building a PHP-based portfolio site without any frameworks whatsoever. I have created an index.php file in the root and a lot of folders namely /about, /contact, /portfolio and the like. Within each of those, I have a separate index.php file
I created a .htaccess file and in it, I have this code...
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
For some reason when I visit my site at example.com/index.php or example.com/about/index.php the .htaccess file is not working and removing it.
Any ideas why?
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If the intention with these directives is to remove index.php from the requested URL then you are using the wrong rules! This rule would route a request for /something to index.php/something (passing the requested URL-path as path-info to index.php) providing /something does not map to a file or directory.
You have presumably structured your URLs so they map to filesystem directories from which the DirectoryIndex is served, so the above directives would seem to be entirely redundant. (?)
To remove index.php from the end of any URL
To remove index.php (the DirectoryIndex) from any URL you would need to do something like the following instead:
RewriteRule ^(.+/)?index\.php$ /$1 [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
To clarify...
you must not be linking internally to /index.php or /about/index.php. The above directive is only for when a user or external site erroneously requests the index.php file directly.
And include trailing slashes on your internal links. eg. you should be internally linking to /about/ and /contact/ etc. (not /about or /contact), otherwise mod_dir will implicitly issue a 301 redirect to append the trailing slash.
It took a while and thanks for the suggestions by Mr White this is my solution..
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Works perfectly now.
Thank you for everyone who took the time out to help me on this.

Infinite amount of rediretcs with .htaccess in subdirectory

I'm trying to get a webpage running which might not get any support anymore, so I have to fix this on my own. So in the root directory, I have this .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*) resources/$1 [L]
So all the content gets redirected to the resources/ folder. Now this kinda works, as the frontpage shows all the static content (CSS, JS, Images). But when I try to open mydomain.com/admin, I'll get an HTTP 500. The logfile says
AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
The .htaccess file in the sub-dir has this content:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?param=$1 [QSA,L]
For my basic Apache knowledge this does seems quite right; but is it, or do I have change Apache settings? (The VPS is a kind of PaaS-thing, so I can access/change some things)
Thank you.
Have your root htaccess rule file in following way(you need to put a condition for a check before rewriting).
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/resources [NC]
RewriteRule ^(.*)$ resources/$1 [L]
Then for your sub directory level htaccess file try following:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?param=$1 [QSA,L]
Also in case you want to apply same rules which you have in your root directory to sub-directories then add RewriteOptions InheritBefore before RewriteEngine ON in your sub directory level htaccess file.

HTACCESS redirect for multiple Vue router applications depending on URL

Running three vue.js applications with vue-router on apache sever.
The HTACCESS redirects all non existing paths to /folder1/index.html. Now I want to include a redirect when the user accesses /folder2/success > /folder2/index.html instead of folder1/index.html
My current HTACCESS file looks as follows, however it isn't working it still redirects to folder1.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder1/index.html [L]
RewriteRule /folder2/success /folder2/index.html [L]
</IfModule>
Really stuck on this any suggestions would be greatly appreciated!
Rules are executed in the order they are written. Since /folder/success does not exist as a file or directory, it is rewritten to /folder/index.html, which does not match your rule for /folder2/success.
Furthermore, since this is in a .htaccess file, it is in the context of that folder. The prefix of that folder is always removed from the part of the url that is matched, which means that the part that is matched never begins with a slash. You should be matching against folder2/success instead.
Your .htaccess file would look something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteRule ^folder2/success /folder2/index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder1/index.html [L]
</IfModule>

How to add redirect in htaccess for all urls not cotaining specific string?

I'm having some htaccess issues with my wordpress blog. My previous url's were something like
article-name.html
Now I changed the structure to
blog/article-name.html
But I'm still getting 404 for old url's I shared on various other sites. I've tried adding in htaccess a rule, also tried "redirection" plugin with no success:
and in .htaccess I tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).)*.html$ - /blog/$1.html [L]
</IfModule>
Your regex appears to be a problem, try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).+?\.html)$ /blog/$1 [L,R=301,NC]
Change the RewiteBase
But your .htaccess is not an Original WordPress .htaccess. Why you dont use that?

mod_rewrite - Send everything that doesn't exist to index.php

I would like to have my .htaccess file rewrite anything that doesn't exist to the index.php file. So for example: www.example.com/category/subcategory/product1/ would be rewritten to index.php?request=category/subcategory/product1
I want to perform a check to see if the directory exists first though, and if it does, do not rewrite. I have something like this at the moment, but just need to know how I can get the requested URL into the PHP $_GET variable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php [L]
Advanced thanks for any help!
You are almost there:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]
TBH, there is no real need to put requested URL into $_GET variable -- you can ALWAYS access original (requested) URL via $_SERVER['REQUEST_URI'] -- the only difference is that REQUEST_URI will always start with leading slash (e.g. /category/subcategory/product1).

Resources