htaccess redirect virtual directory - php

Can't figure out what I'm doing wrong.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)\.php$ detail.php?name=$1 [NC,QSA,L]
RewriteRule ^(.+)/directory/\.php$ detail2.php?name=$1 [NC,QSA,L]
The first RewriteRule should redirect anything (ending on php like domain.com/product1.php) from the root domain to detail.php (it can not affect things like domain.com/contact.php)
A 2nd RewriteRule should redirect anything from domain.com/directory/product-b1.php to detail2.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /foldernamehere/index.php/$0 [PT,L]
paste this code in your .htaccess file it will resolve your problem

Related

RewriteCond exclude folder

I have the following in my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
This works nicely, but I don't want the RewriteRule to trigger for the following folder and its subfolders.
www.site.co.uk/Content
How do I do this?
Try this Rule,
RewriteEngine on
RewriteRule ^(content)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
or create a .htaccess in content directory and put
RewriteEngine off

PHP Pretty URLs combination of two mod_rewrite rules?

I have this .htaccess code in the root folder to replace this url: localhost/mysite/profile.php?username="someone" to become localhost/mysite/someone.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/profile.php?username=$1
This is working fine but what I want is to include subpages like for example the about page to be localhost/mysite/about instead of localhost/mysite/about.php. And the subpages are in the root folder too.
How can I achieve these two rewrites?
You can have this code in mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
# add .php internally to files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handles profile URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]
Try below code it's working fine to me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try This
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/([A-Za-z0-9-]+)$ /mysite/profile.php?username=$1

.htaccess: Rewrite dynamic url with static url

I want to enter http://localhost:81/admin/dashboard in my browser but the request should be http://localhost:81/admin/index.php?page=dashboard.
The mod_rewrite is enabled and i tried this in the .htaccess but it didn't work. The .htaccess is located in htdocs/admin/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?page=$1 [NC]
You can match the trailing slash optionally by adding a ? next to it in the pattern :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [NC]

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

PHP .htaccess exception some of .php file

I have .htaccess that the function is to hide .php extension and convert user.php?user=username to be :
http://example.com/hidayurie
Now I have a problem, I have file search.php. When I run the URL : http://example.com/search?q=username. It still detect that search is username whereas I have file search.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
How can I set if .php file, then it will open that php file without extension?
Try adding some conditions to your rules to prevent the rewrite if the URI points to a file that exists. You should also make sure you have multiviews turned off (this can be anywhere in your htaccess file):
Options -Multiviews
Then add these conditions before each of these 4 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
You need to define search and any other different pages/URLs you wish to rewrite, otherwise yeah it will continue to think it's a user and refer to user.php
Example:
RewriteRule ^search/?$ /search.php
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ /search.php.php?q=$1
Second line would allow you to query your search with a clean url, ex; yoursite.com/search/username/

Categories