.htaccess REQUEST_URI only one character redirect - php

I'm basics to htaccess. I have an issue with htacess SEO Friendly URL.
For Ex:
If the URL is like www.example.com/A then htaccess should redirect to browse-category.php?alphabet=$1
My Code, but not working:
RewriteCond %{REQUEST_URI} [a-zA-Z]{1}$
RewriteRule ^([a-zA-Z0-9-/]+)$ browse-category.php?alphabet=$1
If URL is like this www.example.com/add-business then it should redirect to $1.php.
Please help me and thanks in advance

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# if a matching .php file exists then internally forward /file to /file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
# otherwise forward to browse-category.php?alphabet=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ browse-category.php?alphabet=$1 [L,QSA]

Related

How to remove .php extension only for pages under a sub directory?

I am trying to remove .php extension for only pages under a subcategory folder of the website.
currently: example.com/blog/my-first-blog.php
what i want: example.com/blog/my-first-blog/
I have tried the following rule in .htaccess (placed in root) but it still shows .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/blog/$1.php -f
RewriteRule ^(.+?)/?$ /blog/$1.php [L]
The best way to achieve result you want is building a routing.
Firstable, you need to rewrite all traffic to your index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
Then, you need to write router that will be parsing URL paths.
The simplest example of router is:
$url = urldecode(preg_replace('/\\?(.*)$/', '', $_SERVER['REQUEST_URI']));
if ($url == '/contact/about') {
include 'contact.php';
}
You can check how does it work in PHP frameworks to get a better solution, or use one of them.
I figured it out
I just had to create .htaccess file under the "/guide/" directory. with the following rule note RewriteBase /guide/ for making sure it only removes .php under "/blog/" folder
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try this --
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.example.in/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.php $ http://www.example.in/$1 [R=301,L]
Try this
DirectorySlash Off
Options -MultiViews -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

Get URI params without index.php

I have a URL like http://example.com/index.php/fruit/1a2b3c
I want to get the URI's for which I have written a code.
Now I want to remove the index.php from the visible URL, it should work even when the URL is http://example.com/fruit/1a2b3c and it should still point to index.php
I am on apache2 using PHP 7
Add following code to your .htaccess file.
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]
change your .htaccess file to,
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
hope this will work.

RewriteRule rule for old url to new url add question mark in the url codeigniter

I want to redirect residential-property/20/old_value to /residential-properties/new_value for this I have written code in .htaccess file :
RewriteEngine On
RewriteBase /
RewriteRule ^residential-property/20/old_value?$ /residential-properties/new_value? [B,NE,R=301,L,NC]
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Actually 301 direct all index.php requests
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
# this gets rid of index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
The above code redirect the old url but it also add ? in the url like this https://example.com.com/?/residential-properties/new_value/, I don't want to add that question in the url what i am doing wrong please help me on this. Thanks in advance.

mod_rewrite and apache clean hyperlinks

i've a php website and using mod_rewrite for clean urls.
my files:
villa.php about.php contact.php index.php 404.php
my villa.php is dynamic and works with mysql db. url example:
domain.com/villa?i=2&s=villaname
i want to use it like domain.com/villa/id/villaname
my .htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
but its not working. what is the right configuration for .htaccess file? any help would be great. thanks for your time.
You can use this rule in your htaccess file
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,NC]
RewriteRule ^yazlik/(.*)/(.*)$ yazlik.php?i=$1&s=$2[L,NC]
RewriteRule ^villa/([0-9]*)/(.*)/?$ /villa?i=$1&s=$2 [QSA,NC,L]

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