Htaccess how to add static text to url - .htaccess

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]

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.

mod_rewrite a sub directoy to child subdirectory

I have a following sub domain and folder structure
https://subdomain.maindomain.com/companyName1/area/dashboard
I want to mod_rewrite to this
https://subdomain.maindomain.com/companyName1/area/
So that everything in "dashboard" loads, in "area"
I would like the .htaccess file to live in the area folder, as there are multiple companyNames
I've tried placing the following .htaccess in companyName1/area
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/dashboard/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /dashboard/$1 [L]
However when visiting https://subdomain.maindomain.com/companyName1/area/ it just loads the index.html of that folder, and not of the /dashboard folder
With your shown samples, attempts please try following htaccess rules. Please make sure to keep these rules at the top of your htaccess file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /companyName1/area/
##For external redirect to area link.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s.*/dashboard/?\s [NC]
RewriteRule ^(.*)/dashboard/?$ /$1/? [R=301,L]
##For internal rewrite to dashboard folder here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

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]

Rewrite htaccess url and remove extension

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]

301 All aspx files to a Subdomain

We just redesigned a site for a client in EE, located at example.com (with and without www.). Their original site is ASPX. They've still got a number of ASPX pages that they want to keep, so their IT people created a subdomain, www2, which is basically a clone of their old site.
I need an htaccess rule that will check if the requested page ends in .aspx, then redirects to the www2 subdomain. It should also make sure that the requested page doesn't exist
I tried using the following rule, but it doesn't work.
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
My htaccess file (including the above rule) looks like this:
RewriteEngine On
# redirect all .aspx pages to www2
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
# strip index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Does anyone have a solution for this?
The RewriteRule directive does only test the URL path. If you want to test any other part of the requested URL, you need to use the RewriteCond directive:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]

Resources