My .htaccess file should remove .php extensions in a url, force a slash and assign get values where required. Where a script/file/directory is not found, it redirects you to the root. For example it works perfectly if you type http://127.0.0.1/about-us/ it takes you to http://about-us.php or if you type http://127.0.0.1/file/delete-avatar/230 it rewrites to http://127.0.0.1/file.php?a=delete-avatar&id=230, or if you type http://127.0.0.1/nu (where nu is an existing directory) it takes you to http://127.0.0.1/nu. However when you put a script inside a directory and append a GET value the .htaccess fails to rewrite and redirects to the index. For example, if you type http://127.0.0.1/nu/login/recover-password does not rewrite to http://127.0.0.1/nu/login?a=recover-password. My .htaccess is here
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# remove php extensions (only for GET method)
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1/? [L,R=301]
# don't touch other existing files/folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# force trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1/ [L,R=301]
# rewrite extensionless php files
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/$ $1.php [L]
#subscribe
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?a=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]
# finally, if not found
RewriteRule ^ index.php [L]
</IfModule>
Insert this new rule just below #subscribe line:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+/[^/]+/[^/]+)/([^/]+)/?$ $1.php?a=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+/[^/]+)/([^/]+)/?$ $1.php?a=$2 [L,QSA]
Related
This is my htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^https://example.com%{REQUEST_URI} [NS,R,L]
RewriteRule ^((?:[a-zA-Z0-9_-]|%20)+)/?$ member.php?id=$1
With this htaccess, i can remove .php and i can show my "example.com/member.php?id=1" page as "example.com/3".
Now i want to show "example.com/product.php?id=1" as "example.com/product/1" but the interesting part, there is not exist "product" file. Is it possible to show as "example.com/product/1"?
Thanks
Have it like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# product rewrite
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L,QSA,NC]
# member rewrite
RewriteRule ^([^/]+)/?$ member.php?id=$1 [L,QSA]
I have my project in my htdocs folder where my main file is index.php. I have tried multiple time to remove the .php extension when the file is opened in the browser, using the .htaccess file with the following lines without luck:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
The extension remains. Is it because the link in my browser is
file:///C:/wamp64/bin/apache/apache2.4.23/htdocs/Cipcpr/index.php
and not
localhost/...
You can use:
Options -MultiViews
RewriteEngine on
# remove php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# rewrite with php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
If your project doesn't need to remove extensions dynamically, you can manually set the RewriteRules for every file. Here is the code for your index.php
RewriteEngine On
RewriteRule ^index/?$ index.php [NC,L]
when you now type /index it will reference to /index.php file.
To add other file (let's say login.php) you can just copy last RewriteRule and rewrite the indexes.
RewriteRule ^login/?$ login.php [NC,L]
Just add .htaccess file to the root folder of your site(for example, /home/domains/domain.com/htdocs/) with following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I need to 301 redirect all URLs ending in .php to the non extension version. But the rewrite rules need to work with the existing ones below.
# ==== REWRITE URLS ====
RewriteEngine On
# pass through root
RewriteRule ^(index\.php|Sitemap\.xml)?$ - [L]
# no more / so add extension
RewriteCond $1 !/
RewriteCond $1 !\.php$
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(_assets|_css|_fonts|_includes|_scripts)($|/) - [L] #exclude these folders using the 'last: L' flag
RewriteRule ^(.*)/(.*)$ /$1-$2 [L]
This is an extension of this question here.
So the desired result is:
domain.com/region/brand/more-content-here.php
Redirects permanently to:
domain.com/region/brand/more-content-here
But fetches the actual file at:
domain.com/region-brand-more-content-here.php
Tried a few various ideas but they didn't seem to work in with the existing rules and htaccess is not my strength. Also need it to be querystring friendly. Any help would be appreciated.
To remove the .php extension ,you can use :
RewriteEngine on
#1)Permanently redirect "foo.php" to "/foo"#
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
#2)Rewrite "/foo" to "/foo.php"#
#this will internally redirect file to file.php#
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Working code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I need to forward/redirect a domain from
http://local.abcxyz.com/be/country/CN/BE
to
http://local.abcxyz.com/be/country/visum-china/BE
How can I do it using .htaccess ?
my current htaccess file looks like this
RewriteEngine On
# If the file or directory exists, show it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
# Blank queries get sent to the index
#RewriteRule ^$ index.php [L]
# All other queries get sent to the index as index.php/whatever
#RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond $1 !^(index\.php|images|swf|uploads|js|css|robots\.txt)
RewriteRule ^(.*)$ /be/index.php/$1 [L]
this htaccess exist in be folder.
You can use this code in your DOCUMENT_ROOT/be/.htaccess file:
RewriteEngine On
RewriteBase /be/
RewriteRule ^country/CN/BE/?$ country/visum-china/BE [L,NC]
# If the file or directory exists, show it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond $1 !^(index\.php|images|swf|uploads|js|css|robots\.txt)
RewriteRule ^(.+)$ index.php/$1 [L]
Assuming there is no .htaccess in /country/ or any directory below.