Omit folder and few scripts in mod_rewrite - php

I have following problem. My .htaccess looks like this
RewriteEngine On
RewriteBase /phpuserarea/
RewriteCond %{REQUEST_URI} !^mod/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
ErrorDocument 404 /phpuserarea/404/index.php
But whenever I try to directly access anything in the mod directory the server responds with 404.
Other problem is, that besides the index.php I want to allow direct access to let's say testa.php and testb.php
Those are my ajax files and I can't seem to find a solution.

Have it this way:
ErrorDocument 404 /phpuserarea/404/index.php
RewriteEngine On
RewriteBase /phpuserarea/
RewriteCond %{REQUEST_URI} !/mod/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
You need leading slash in RewriteCond %{REQUEST_URI} and better not use anchor ^ as your .htaccess is inside /phpuserarea/.
Also you don't need to use ?%{QUERY_STRING} in target since query string is automatically passed over to target if your rule doesn't overwrite it.

Related

Same IP, multiple Laravel installs in sub directories. How to rewrite using .htaccess?

I am trying to figure out (learn) how to set up .htaccess so multiple Laravel installs could be run on the same IP by by using the URL (/site_1, /site_2) like:
http://xx.xx.xx.xx/site_1
So far what I have realized that subsequent request for resources (like: /css/app.css) don't pass the rewrite rules and are lost. I have tried the OR flag to tie the request somehow to the url but then rules gets mixed up.
How could I get this to work?
.htaccess
RewriteEngine ON
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} /site_1 [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]
RewriteCond %{REQUEST_URI} /aloe [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_2/public_html/public/$1 [L]
Add this block and try again. Hope this will work. It might need some modification for the path you want to access for css and js.
RewriteBase /site_1/public_html/public/
RewriteCond %{REQUEST_FILENAME} \.(css|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]

How to rewrite an url via htaccess

I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]

How to use mod_rewrite to redirect requests of username but rest to a frontend index?

I am trying to redirect requests of username to a script but the all rest to a specific index called frontend.php using a .htaccess and a mod_rewrite.
Unfortunately, this failed and I keep struggling:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ frontend.php [QSA,L]
RewriteRule ^/user/([^/]+)$ profile/profile.php?u=$1 [QSA,L]
Thanks for your help!
You can use:
RewriteEngine On
RewriteRule ^user/([^/]+)$ profile/profile.php?u=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ frontend.php [L]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Have you tried to swap the last 2 lines against each other?

.htaccess mod_rewrite to a separate script if it ends with a string

I'm writing a simple CMS that will rewrite http://host/path/to/resource to http://host/cms/render.php/path/to/resource (unless a file exists at that location, e.g. an image).
How can have it redirect to a different script if the request ends with a certain string? For example:
http://localhost/path/to/resource --> cms/render.php/$1
http://localhost/path/to/resource.admin --> cms/admin.php/$1
This is what I've tried, but I just get an internal server error:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} = ^(.*)\.admin$
RewriteRule ^(.*)$ cms/admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ cms/render.php/$1 [L]
Thanks!
My bad. It doesn't need to be a condition in this case:
RewriteEngine on
RewriteRule ^(.*)\.admin$ cms/admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ cms/render.php/$1 [L]

MOD_REWRITE but not in sub directories

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]

Categories