Rewrite htaccess rule for a specific file - apache

How can I have rewrite htaccess rule so that a specific file(terms.php) with extension goes without extension(terms) like below
xyz.com/terms.php --> xyz.com/terms
RewriteCond %{REQUEST_URI} ^/terms\.php [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(terms)\.php[?\s] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteRule ^(terms)/?$ $1.php [NC,L]

Related

dynamic url change issue

I want to change my URL from https://website.com/free?s=new-check to https://website.com/free/new-check/
My Current htaccess code :
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
RewriteCond %{THE_REQUEST} /free\?s=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ /free?s=$1 [NC,L]
It works and directed to https://website.com/new-check but what I want is https://website.com/free/new-check/
previously I removed the .php extension from the URL.
I tried a lot of solutions but nothing works. please help me
With your shown samples, please try following htaccess Rules. Make sure to keep these Rules at top of your htaccess file if you have other Rules also in your file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##https apply rules here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##External redirect Rule here.
RewriteCond %{THE_REQUEST} \s/(free)\?s=([^\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301]
##Internal rewrite rule here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ free.php?s=$1 [NC,QSA,L]

Opencart - Clean url for specific pages only

I am using Opencart and I have some static pages also.
I don't want to mess up opencart's SEO options so is there a way to create clean URLs only for specific pages?
What I want is :
http://example.com/my-custom-page.php
to
http://example.com/my-custom-page
I am using standart htaccess file comes with opencart package
I have these lines:
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
If you wanted to remove the .php extension in your url just use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
or this
RewriteEngine on
RewriteRule ^(.*)$ $1.php
If .html then you can do
RewriteRule ^([^\.]+)$ $1.html [NC,L]
put the following code at main directory (root) .htaccess file :
RewriteEngine on
RewriteCond %{REQUEST_URI} my-custom-page.php
# the line above to choose only my-custom-page.php and if you remove it
# the code will remove all php pages extensions
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R]
The code will remove my-custom-page.php extension and it would be my-custom-page regardless of its directory

url rewrite in php for change posts url

I want change url:
http://example.com/posts.php?action=view&id="a number"
to
http://example.com/posts/"a number"
I write these into the .htaccess file but they don't work:
RewriteEngine On
RewriteRule ^posts/([0-9]+)/?$ posts.php?action=view&id=$1 [NC,L]
RewriteRule ^posts/?$ posts.php [NC,L]
It'll be like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\?action=view&id=(\d+) [NC]
RewriteRule ^posts\.php$ /%1/%2? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\ HTTP [NC]
RewriteRule ^posts\.php$ /%1/? [R=301,L,NC]
RewriteRule ^(posts)/(\d+)/?$ /$1.php?action=view&id=$2 [L]
RewriteRule ^(posts)/?$ /$1.php [L]
You can try below in your .htaccess file
.htaccess file code:
RewriteEngine On
RewriteRule ^posts/([0-9]+) /posts.php?action=view&id=$1 [QSA,L]
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite.
L means if the rule matches, don't process any more RewriteRules below this one.

htaccess rewrite url redirect php file to folder

I want to redirect this php url to the folder.
I always used the settings below. But since the update of apache to 2.4 it doesnt work anymore.
Goal:
example.com/about.php?sub=about or example.com/about.php?sub= should be seen in the browser as example.com/about/
I use the following settings.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(example.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
RewriteRule ^about/$ about.php$1 [L]
RewriteRule ^about/([A-Za-z0-9-]+)$ about.php?sub=$1 [L]
This is what I am using now...
I don't see any rule in your code that is making /about.php?sub= to /about.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(about)\.php(\?sub=[^\s&]*)?\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^about/?$ about.php?sub=about [L,NC,QSA]
RewriteRule ^about/([a-z0-9-]+)/?$ about.php?sub=$1 [L,NC,QSA]
# Add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

Simple URL Rewrite Problems

I'm having trouble re-writing a simple URL with .htaccess.
The URL I am trying to rewrite is:
http://www.domain.com/index.php?page=PAGE_NAME
I would like the PAGE_NAME to be directly after the domain, for example if PAGE_NAME is blog:
http://www.domain.com/blog
At the moment I have tried the following with no success:
RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1
All help is really appreciated.
Thanks in advance.
My current .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
ErrorDocument 404 /search.php?page=notfound
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite main page URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]
# Rewrite download URLs
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1
# Rewrite page navigation links
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2
# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
#send request to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]