.htaccess url rewriting - exceptions for certain files only - .htaccess

This is my .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]
Basically if you enter /test, it will work perfectly, and if you go to /test.php, it will show the PHP page without the rewrite rule, which is what I need for certain pages only.
let's say there are some pages (a few) where I would want this to be able (to load the whole .php page), but ALL the other pages, what I want is that if someone goes to /contact.php, it redirects to /contact, to the right one.
So let's say for "result.php" and "search.php", I want it to be able to load the whole PHP page, but for the rest, I want it to redirect to the right adress (so /contact.php => /contact).
How could I do it? Basically I need to add this RewriteRule ( RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]) only for a few pages (that I would determine myself), and for the rest of the pages I need another RewriteCond (that redirects all the "test.php" to "test")
Thanks a lot!

You can put all those exception in your first RewriteCond and alter 2nd RewriteCond to skip real files and real directories like this:
RewriteCond %{REQUEST_URI} !/(index|result|search)\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [QSA,L]

Related

.htaccess file hide GET request in URL

I am very new to .htaccess files and may have done mine wrong. I have been gathering snippets of code trying to get done what I want.
This is what I have in my file so far
I read this should be in your .htaccess file
Options +MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
This was added to force a redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This was added to remove .php at the end of all the files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This was added to remove the showing of /index when the home link was pressed
RewriteCond %{THE_REQUEST} ^.*\/index?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L]
I have tried a lot of ideas I found on both here and other sites from Google. I have had no luck hiding the GET request.
Currently, the URL looks like https://mysite.ca/event?id=123
What I would like it to look like is either https://mysite.ca/event/123
This is hosted on Godaddy and it is just a plain PHP site if that makes any difference.
Any ideas would be great!
First of all Options +MultiViews already removes .php extension so you don't need:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
About your problem try this Rule:
RewriteRule ^event/([0-9]+)?$ event?id=$1 [NC]
Which changes url from example.com/event/<number> to example.com/event?id=<number>
Although, if your site makes infinite redirects then you should change it to:
RewriteRule ^events/([0-9]+)?$ event?id=$1 [NC]
This happens if you have some includes/headers that redirect back to event and creates infinite loop.

.htaccess rewrite for url parameters?

i have been trying to make my urls "pretty" / human readable, the urls at the moment are:
[BASE_URL]/?action=viewProposal&proposaltitle=tesst
I want to rewrite them to be just [BASE_URL]/tesst
I tried using the following code and modifying it but it wouldn't work, ie. it didn't redirect the pages but didn't throw any errors.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Will the PHP GET functions still work properly as ?action defined whether its a view / edit / delete?
I am assuming base url your index.php try following rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?action=viewProposal&proposaltitle=$1 [QSA,L]

.htaccess correct complex redirection

I am attempting to create an seo friendly url system.
Sample:
/category would get information from cat.php?cat=category
My issue is that I want to keep that url and be able to do something like the following
/category?page=2&sub=1
or
/category/?page=2&sub=1
I was able to get the folders to redirect to the cat.php file but I cant seem to figure out the second half.
#Create redirect for all nonexistant folders to the category file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteRule ^(.*)/? cat.php?cat=$1 [L]
If I am understanding you this should be what you need.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]
So if you were to use a URL like this
http://www.yoursite.com/category?page=2&sub=1
The RewriteRulewill direct it to cat.php?cat=category&page=2&sub=1
You're lacking the QSA flag at the very least. Without QSA, the existing query string is simply replaced by the new query string.

Mod re-write on sub folder

I'm trying to mod re-write to a specific sub folder called /checkout this has an SSL certificate attached for https
So I have this in the root folder of my site -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
# this kinda works below but is resolving to index2.php and is showing in the url so not really working yet
RewriteRule checkout/([^/]+)\.html$ https://mysite.com/checkout/index2.php [L]
Except that instead of showing something like
https://mysite.com/checkout/index.html
it shows
https://mysite.com/checkout/index2.php
(index2.php is a test url btw)
I've also tried amending the .htaccess in the sub folder to to something like
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
RewriteRule ^([^/\.]+)/?$ https://mysite.com/checkout/index2.php [L]
I just want to process
https://mysite.com/checkout/index.html or https://mysite.com/checkout/about-us.html as the same I would in the root like this
https://mysite.com/index.html => https://mysite.com/index.php (and then get the variables etc)
ie
RewriteRule ^/?(catalogue-page)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+) index2.php?friendly_url=$1&catalogue_id=$2&view=catalogue&categoryid=$3&subcategoryid=$4&subjectid=$5&item=$6
I think that https is more complex.
Any pointers/help etc much appreciated
Your RewriteRule seems to indicates that you are trying to take a non-secure url and display a different page via https, this in itself is not possible. Having said that your description doesn't sound like that is the overall aim.
If you wish to force non-secure URLs to be secure, this is fine and it's also fine to want requests for index.html to server index.php. It's not however possible to do this all in one fell swoop.
# first force urls to be secure.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^checkout/([^/\.]+)/?$ https://mysite.com/%{REQUEST_URI} [R=301,L]
# now rewrite them, so that the requests all get served through the front controller
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^checkout/([^/\.]+)/?$ /checkout/index2.php [L]

Configure htaccess to load static pages

Im working on a server with the following htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
RewriteRule ^[^/]*\.html$ index.php
RewriteRule ^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/.*$ - [L]
Now they asked me to prepare a static page inside their server, lets call it http://www.myserver.com/mystaticpage.html
The problem is that when i try to access that url, it redirects to index.php. How could I alter the htacces file to address this problem without messing anything with the installed CMS?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond ${REQUEST_URI} !^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)
RewriteRule .* index.php
The rules you used before were redundant: if .* is rewritten to index.php then why also rewrite ^[^/]*\.html$ index.php to it? it already matched previous rule...
They also overlapped - since the three RewriteCond conditions were only applied on the first rule. So the second rule was also applied to static files on disk.
Also, the two rules that were listed last had no effect whatsoever. Either you needed to list them first, or not at all. I converted them to an additional RewriteCond since they were only attempted to avoid rewrite on certain uris

Resources