Rewrite htaccess url and remove extension - .htaccess

I'm trying to rewrite and remove extension
http://www.example.com/pages/cart.php
to
http://www.example.com/cart
My htaccess file is located in the public folder.
RewriteEngine On
RewriteRule (.*) pages/$1 [L]

With your shown attempts, please try following htaccess Rules. Make sure you keep your htaccess file along with pages folder(not inside it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/pages/(cart)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^(.*)/?$ pages/$1.php [L]

Related

.htaccess redirect non-clean URL with two variables to clean URL

I am trying to redirect www.bostonmabl.com/400hitter/boxscore/?SeID=332&GmID=93091 to www.bostonmabl.com/400hitter/boxscore/332/93091/ in the event that a user enters an old non-clean URL. Below is my .htacess file which I tried placing in both the 'root' directory and the '400hitter' directory, yet neither is working. Any help is greatly appreciated.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^SeID=([^&]+)&GmID=([^&]+) [NC]
RewriteRule ^/400hitter/boxscore/index\.php$ https://bostonmabl.com/400hitter/boxscore/%1/%2/? [R=301]
With your shown samples, please try following htaccess rules. Make sure your .htaccess rules file and index.php are residing in same folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect to new URL in browser.
RewriteCond %{THE_REQUEST} \s/(400hitter/boxscore)/\?SeID=([^&]*)&GmID=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^400hitter/boxscore/(\d+)/(\d+)/?$ index.php?SeID=$1&GmID=$2 [QSA,NC,L]
NOTE: This solution assumes that you want to rewrite(in backend) to index.php(by seeing your attempted code) and pass SeID and GmID parameters/query string into it.

Rewriterule for PHP files fails

I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
For example:
RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L]
RewriteRule ^admin/([a-z-])/([0-9a-z-#._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L]
I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error
The requested URL was not found on this server.
The full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
Files locations are:
1. C:\wamp64\www\test.com
2. C:\wamp64\www\test.com\admin\add_news.php
C:\wamp64\www\test.com\admin\edit_news.php
3. I want redirect from URLs with underscores to hyphens, for example:
from admin/add_news.php to admin/add-news
Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id
Example: admin/edit-news/my-news-title/2
Any ideas how to fix it? Thank you.
With your shown attempts, please try following htaccess rules file.
Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.
Samples specific rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L]
Generic rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L]

RewriteRule for hiding file extension and change file name

starting url - example.com/vid.php
wanted url - example.com/video
any help, pls, to write the RewriteRule
RewriteCond %{THE_REQUEST} /vid\.php\s [NC]
RewriteRule ???
With your shown samples, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also make sure that your vid.php file is present alongside with htaccess rules file.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/vid\.php\s [NC]
RewriteRule ^ video? [R=301,L]
RewriteRule ^video/? vid.php [NC,L]
You don't even need a separate RequestCond here...
RewriteEngine on
RewriteRule ^/?vid\.php$ /video [QSA,R=301,L]
RewriteRule ^/?video/? /vid.php [QSA,END]
Test with a fresh anonymous browser window.
It is a good idea to start out with a R=302 temporary redirection and only change that to a R=301 permanent redirection once you are satisfied.

Htaccess how to add static text to url

I want to make example.com /productName to example.com /product/productName
I just want to add static text /product/ in mid to url how can i add this with .htaccess file to url structure? I already tried this code but it did not worked.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#hiding parameter name and product.php
RewriteRule ^([^\.]+)$ /product.php?productName=$1 [NC,L]
With your shown attempts, please try following htaccess Rules. Please make sure to keep your htaccess along with folder product(not inside product folder).
Make sure to clear your browser cache before testing your URLs.
##Making rewrite engine on
RewriteEngine ON
##rules for external redirect
RewriteCond %{THE_REQUEST} \s/(product)\.php\?code=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rules for internal rewrite here.
RewriteRule ^(product)/(.*)/?$ $1.php?code=$2 [QSA,L,NC]

.html files not accessible in Magento when added to root folder

I'm running a Magento site and needed to upload a google site ownership confirmation file to the root. When I did this, html file was not accessible via url when typed as follows www.website.com/google0564e497cc4446t6.html instead I got a 404 page. Other file formats work just fine. I concluded that there must be a .htaccess redirect.
This is the .htaccess content:
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
## RewriteRule ^(.)ItemDesc.asp?IC=(.)$ $1$2 [L]
## RewriteRule ^supply-items/$ ItemDesc.asp?IC= [NC,L]
RewriteRule ^([^/]*)\.html$ /ItemDesc.asp?IC=$1 [L]
##RewriteRule ^ItemDesc.asp?IC=$ supply-items/ [L,R=301]
RewriteRule test_rewrite\.html http://www.website.com/rewrite_successful.html [R=301,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As it turns out the previous website redirects are very important for maintaining of SEO, so I cannot just remove them. However, is there a way to make an exception to specific .html files such as www.website.com/google0564e497cc4446t6.html using the .htaccess file?
Thank you!
You can modify the first part of your rule and add a condition to skip the google file.
RewriteCond %{REQUEST_URI} !^/google(.*)\.html$
RewriteRule ^([^/]*)\.html$ /ItemDesc.asp?IC=$1 [L]

Resources