Help with htaccess RewriteRules - php

I am posting this question again because I didn't explain clear enough. Here are things that I need to achieve with the .htaccess file:
Re-route /public_html/ to /public_html/myfolder/
Make website/index.php?q=param to website/param/
My php files are inside /public_html/myfolder/ and my image files are inside /public_html/myfolder/images/
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule .* https://www.website.com/$1 [L,R=301]
RewriteRule ^$ myfolder/index.php [L]
# Rewrite rules
<IfModule mod_rewrite.c>
#RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
</IfModule>
The problem is, it redirects fine.. but I can't see any images! It doesn't seem to load my js, css, and etc..
This is the error I get:
1<br />
2<b>Notice</b>: Undefined property: stdClass::$component in <b>/home/website/mypage/includes/Template.class.php</b> on line <b>31</b><br />

I would do something like this. I'm thinking the problem could be the order of the rules, that say, the first rule will try to load the image/js/css since isn't looking if the file exists.
try this:
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule .* https://www.website.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myfolder/index.php?q=$1 [L,QSA]
RewriteRule ^$ myfolder/index.php [L]

Related

Rewrite URL dont get values and error 404

My Htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ %2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+?)-([0-9]+)$ index.php?id=$2&title=$1 [L]
</IfModule>
I have in php urls as this :
index.php?id=34234&title=Hello_World&name=Mike
In the link i put this :
TEST
And i try get this url
http://www.test.com/34234/hello_world/mike
But all time give error as 404 error and i don´t know which it´s the solution for this, Thank´s in advanced
Your original URL has 3 path segments while your rule accepts only two. You need to fix your regex patterns
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^/]+)/(.+)/?$ index.php.php?id=$1&title=$2&name=$3 [L]
</IfModule>

Htaccess rewrite rule causing Forbidden access

I'm trying to hide subfolders from url like this:
localhost/imo/public_html/public/ajuda.php to localhost/imo/ajuda
Its working well if the file exist, if not i get a forbidden access like this:
You don't have permission to access
/imo/public_html/public/public_html/public/public_html...public/contacto.php.php.php.php.php.php.php.php.php.php
on this server.
this is my htaccess file:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^$ public_html/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ public_html/public/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA,NC]
</IfModule>
I already search on stackoverflow and it seems that the rules is causing a loop but the solutions provided didn't work on my case so far. Any help at all would be very aprecciated.
Thank you.
UPDATE
Thanks to #Matthijs Otterloo this is the working htaccess script:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^$ public_html/public/
RewriteCond %{REQUEST_URI} !^/public_html/public
RewriteRule ^/?([^/]+)$ /public_html/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA,NC]
</IfModule>
Try something like this in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/public
RewriteRule ^/?([^/]+)$ /public_html/public/$1 [L]
Best way would be to set-up propper virtual hosts but this should fix it.

.htaccess rule appending index.php instead of URI

My .htaccess looks like this..
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/members/invite/$1 [R,L]
</IfModule>
I want to be able to redirect an URL that looks like this..
https://sub.domain.com/ZTHF76
to...
https://www.domain.com/members/invite/ZTHF76
But instead I am getting
https://www.domain.com/members/invite/index.php
I need to keep the first rule to remove index.php from the URI of the rest of the application.
Any help with this greatly appreciated.
You should reorder your rules and keep redirect rule before rewrite one:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/members/invite/$1 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Htaccess rewrite with id

i got a problem with rewriting my edit.php?id=1 to edit/id/1. Right now i have the following in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^edit/id/([^/.]+)$ edit.php?id=$1 [NC]
This doesn't change the url. Can something see what i do wrong?
You need one additional rule to change the URL externally. This should be placed in root .htaccess:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/([^/]+)/edit\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1/edit/id/%2? [R=302,L,NE]
RewriteRule ^([^/]+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L,QSA]
Give this a try and see how it works for you.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L]
</IfModule>
OR you can do this if you only have a couple of directories the same. The will go in the root .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profiles|customers|test)/edit/id/([^/.]+)$ $1/edit.php?id=$2 [NC,L]
</IfModule>

.htaccess file with some exceptions

I need a .htaccess file that follows these rules:
all files (and subfolders/subfiles) of the folders images, css, js, admin, fonts should be reachable
urls like bla.com/info should be redirected to bla.com/index.php?p=info
urls like bla.com/products/22 should be redirected to bla.com/index.php?p=products&cat=22
the url bla.com/products should be redirected to bla.com/index.php?p=products
Can anyone help me with this? This is what I got so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /staging/
RewriteRule ^favicon.ico favicon.ico [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?p=$1 [NC,L]
But it only works for the first 2 conditions...
Thanks!!
These 2 rules should work for you:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([a-z0-9-]+)/?$ index.php?p=$1 [NC,QSA,L]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?p=$1&cat=$2 [NC,QSA,L]
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /staging/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)(?:/(.*?)/?|)$ index.php?p=$1&cat=$2 [L,QSA]

Categories