adding .php extension if not exist - .htaccess

I have the below rewrite but I want to replace the URL without extension .php if the user types the URL with .php at last, how can I revise the below rewrite script?
# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]

With your shown samples, please have your .htaccess file with following Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
# metchants/yyy/index.php to redirect /m/yyy rule
RewriteRule ^merchants/([\w-]+)/index\.php/?$ m/$1 [R=301,L,NC]
# merchants/yyy/abc.php to redirect /m/yyy/abc rule
RewriteRule ^merchants/([\w-]+)([\w-]+)\.php/?$ m/$1/$2 [R=301,L,NC]
# merchants/yyy/xyz/abc.php to redirect /m/yyy/xyz/abc rule
RewriteRule ^merchants/([\w-]+)/([\w-]+)/index\.php/?$ m/$1/$2 [R=301,L,NC]
# To handle /m/yyy/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [QSA,L,NC]
# To handle /m/yyy/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [QSA,L,NC]
# To handle /m/yyy/xyz/abc/index.php rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [QSA,L,NC]

Related

I want to change url by htaccess

i want to change url by htaccess from http://localhost/projectname/cab-details.php?v_name=variable to http://localhost/projectname/variable Here variable is a link(slug) Now My htaccess code is
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^$1/([a-zA-Z-0-9-]+) cab-details.php?v_name=$1
With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /omm/
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/(omm/[^.]*)\.php\?v_name=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal redirect rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^omm/([^/]*)/([^/]*)/?$ $1.php?v_name=$2 [QSA,NC,L]

htaccess doesn't rewrite all rules

The .htaccess file ignores the first rule
The url should be http://example.com/public_html/folder/file.php
# Rewrite all to public_html
RewriteCond %{REQUEST_URI} !php/
RewriteRule (.*) /public_html/$1
# URL: http://example.com/public_html/folder/file
# Hide/Add .php from/to URL
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{REQUEST_FILENAME} !\.(.*)
RewriteCond %{REQUEST_URI} !php/
RewriteRule (.*) $1.php
# URL: http://example.com/folder/file.php
Have it this way inside /Users/gus/sites/New/8888/goestav5/.htaccess:
RewriteEngine On
# ignore all php/ URIs
RewriteRule ^php/ - [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# rewrite every non-file, non-directory to public_html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!public_html/)(.*)$ public_html/$1 [L,NC]

Add some paramaters to url as directory but point to original url with query paramaters

I want that if somebody hits this url:
https://sk.carpul.eu/search?t=0&fp=Bratislava&fla=48.1486&fln=17.1077&tp=Zvolen&tla=48.5762&tln=19.1371&sdate=&stime=&p=1&qh=4b9873ddfb
It will have nice places in url and search rename to spolujazda as:
https://sk.carpul.eu/spolujazda/Bratislava/Zvolen/?t=0&fp=Bratislava&fla=48.1486&fln=17.1077&tp=Zvolen&tla=48.5762&tln=19.1371&sdate=&stime=&p=1&qh=4b9873ddfb
But still server will point to:
https://sk.carpul.eu/search?t=0&fp=Bratislava&fla=48.1486&fln=17.1077&tp=Zvolen&tla=48.5762&tln=19.1371&sdate=&stime=&p=1&qh=4b9873ddfb
I tried in htaccess:
# check if the actual request if for "this1"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?fp=([^&]+)&tp=([^&\ ]+)
# redirect to "this2"
RewriteRule ^ spolujazda/%1/%2/? [R=301,L,NE]
# now rewrite "this2" back to "this1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([^/]+)/([^/]+)/?$ /search.php?fp=$1&tp=$2 [L,QSA]
But not working.
For .php removal I have this in the beginning of htaccess:
Options +FollowSymlinks
RewriteEngine On
## hide .php extension
# To internally forward /dir/file to /dir/file.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
You may use these rules in your site root .htaccess:
RewriteEngine On
# check if the actual request if for "this1"
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?(?:.*&)?fp=([^&]+).*&tp=([^&\s]+) [NC]
# redirect to "this2"
RewriteRule ^ /spolujazda/%1/%2/ [R=301,L,NE]
# now rewrite "this2" back to "this1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(?:spolujazda|search)/([^/]+)/([^/]+)/?$ /search.php?fp=$1&tp=$2 [L,QSA]

Removing part of URL but don't redirect with htaccess

So the problem I'm looking at and haven't managed to solve!
For example the url is http://someurl.com/brands/brand
What I want to accomplish is that htaccess lets te request go to url: http://someurl.com/brands/brand but removes the "brands" part from the url.
Also if the url: http://someurl.com/brands is called the page "brands" needs to be displayed. So only when there is a brand after /brands/ it needs to do a URL rewrite to http://someurl.com/brand
I have tried this but this piece does a redirect to the new location witch doesn't exist.
RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]
So I need the above with out the redirect, it only needs to rewrite the URL.
Any help would be greatly appreciated, Thanks!
This is now my htaccess part where the rewriting is done!
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]

apache mod-rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9a-zA-Z_\-]+)(|\.html|\.php)$ page.php
this is the rule is .htacess.
what I want is if file is not found then redirect to page.php.
for example if url people.php ,people.html is not found then redirect to page.php.
because I also want those filenames without .php like people direct to people.php first. then check file exist or not, if not redirect to page.php.
how do I add one rule to make people redirect to people.php
These rules should work for you:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# if there is no . in request then append .php
# it is important to make [L] as last rule here
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule ^(.+)$ /$1.php [L]
# 404 handling for files that don't exist
# NC is for ignore case comparison
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+\.(html|php)$ /page.php [L,NC]
See if this does what you're looking for:
RewriteEngine on
RewriteRule ^(?:.+/)?([^/.]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:html|php)$ page.php

Resources