.htaccess rewrite url with 2 elements to 1 elemnt - .htaccess

I have a URL with 2 parameters /manufacturer/product.html and I need to redirect to the URL without "manufacturer", ie. /product.html
RewriteRule ^\/manufacturer\/(.*)$ \/$1 [L]
This does not work.
can you help me?

Here is the code that will work for you, make sure you paste this in your root .htaccess file. (rename the current .htaccess file to .htaccess.bak and paste the below code in a new .htaccess file in root directory)
RewriteEngine on
RewriteRule ^manufacturer/product\.html$ /product.html? [L,R]
When everything works as it should, you may replace R with R=301 (permanent redirect).

Related

How to use .htaccess to redirect while maintaining the original URL

I am trying to use the .htacces file to redirect any files or subfolders in a derectory to one file, while still maintaining the original URL input.
So if a user goes to:
https://example.com/folder1/folder2/folder3/
or
https://example.com/folder1/folder2/file.php
it would redirect them back to:
https://example.com/folder1/index.php
but the original URL input would not change.
You can use RewriteRule . In htaccess in the document root add the following rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder1/index\.php$ [NC]
RewriteRule ^folder1/.+$ /folder1/index.php [L]
This will redirect /folder1/foo/bar to /folder1/index.php without changing the url in browser.
The RewriteCond above makes sure you don't rewrite /folder1/index.php to itself (/folder1/index.php) otherwise the rule can cause an infinite loop error.
You can just make :
RedirectMatch 301 ^https://example.com/folder1/ https://example.com/folder1/index.php
This allows you to redirect from the first url in the pattern to the
second one

.htaccess rewrite 301 rules not working

I am in the process of simplifying my URLs and have come up with a problem concerning the use of Rewrite in htaccess files.
Search engines currently list my pages in the following format
www.example.com/x/x/article_a/1159/
which is currently rewritten by the following htaccess file placed in the /x/x directory
RewriteRule walk_a/(.*)/$ /x/x/article_a.php?id=$1
This has worked fine for many years.
I want to simplify the URL to
www.example.com/article-1159-introduction
I have tried the following redirects placed in the root
RewriteRule ^x/x/article_a/(.*)/$ http://www.example.com/article-$1-introduction [R=301,NC,L]
RewriteRule ^article-(.*)-introduction$ /x/x/article_a.php?id=$1
The 301 rewrite seems to have no effect although the second rewrite command works fine.
What am I doing wrong?
Inside /x/x/.htaccess directory use this rule:
RewriteEngine On
RewriteRule ^article_a/(.*)/$ /article-$1-introduction [R=301,NC,L,NE]
Then inside root .htaccess have this rule:
RewriteEngine On
RewriteRule ^article-(.+)-introduction$ /x/x/article_a.php?id=$1 [L,QSA]

.htaccess rewrite from subdirectory

Any help with this would really be appreciated.
I am setting up 301 redirects in a .htaccess file to redirect hundreds of old urls to the latest live pages.
My .htaccess file is currently located in the root of the domain and looks something like this:
RewriteCond %{QUERY_STRING} ^pName=product-one$ [OR]
RewriteCond %{QUERY_STRING} ^pName=product-two$ [OR]
RewriteCond %{QUERY_STRING} ^pName=completely-different-name$
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
While researching for answers on how to bulk redirect to one url using .htaccess, I read that it would wise to change the location of the .htaccess file and place it in the specific directories to which it apply's, so requests to other directories won't even these rules.
So I have placed the .htaccess file in the specific sub directory, which is catalog, but the redirect no longer works. What am I missing here? I want the rules for the urls to be redirected to be placed in the .htaccess file inside the catalog folder so all those rewrite rules wont be loaded each time the root .htaccess is loaded.
I would suggest you change your redirect 301 line to contain the path directly to you catalog folder of OSCommerce.
So instead of using
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
use e.g.:
Redirect 301 /website/catalog http://www.mydomain.com/page2.htm
or
Redirect 301 /catalog http://www.mydomain.com/page2.htm
Option 1) is if your catalog is located at http://youdDomain.com/website/catalog/ .
If it is located at http://youdDomain.com/website/aaa/catalog/ you would use:
Redirect 301 /website/aaa/catalog http://www.mydomain.com/page2.htm
I tested this in my own webserver - hope it helps.
When you put the .htaccess in a subdirectory, you must adjust the RewriteRule pattern accordingly. mod_rewrite strips the directory prefix, where the .htaccess file is located, see RewriteRule - What is matched?.
You must change the rule to
RewriteRule ^product_info.php$ ...
If you have lots of URL paths to rewrite, you might also want to look into RewriteMap.

How to redirect any folders within a subdirectory to a specific file using htaccess?

I want to redirect any folder to a specific php file with the folder name as variable using htaccess, for example:
www.domain.com/subdirectory/folder1 redirects to www.domain.com/subdirectory/file.php?id=folder1
www.domain.com/subdirectory/folder2 redirects to www.domain.com/subdirectory/file.php?id=folder2
This should work if the folders have "/" at their end, for example,
www.domain.com/subdirectory/folder3/ redirects to www.domain.com/subdirectory/file.php?id=folder3
It will be better if I can put the .htaccess file in the subdirectory and get redirect rule according to it.
I created a .htaccess file with the following code while putting the file in the subdirectory:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)$ file.php?id=$1
but it does not work.
RewriteRule ^folder(.*)$ file.php?id=$1
use these i am not tested but it will be work.

htaccess redirect remove /app/webroot in cakephp

I have an htaccess file that I want to redirect the all pages to the another folder. I am using cakephp for the site, except for this folder which I want to redirect.
"/app/webroot/" is added to the new directory so the url is /app/webroot/new/ instead of just new.
I am placing this htaccess file (below) in the "new" directory and want anything at /app/webroot/new/ to redirect to /new/ and remove the /app/webroot/ This folder is independent of cakephp and thus does not need to be processed by cake.
The code below loops and I am not sure why.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /new/
# If your concerned about direct access to a particular page without the sub-dir
# you will want to add something like this
RewriteCond %{REQUEST_URI} !^/new
RewriteRule (.*) /new/$1 [R=301,L,NC]
You should NOT place the .htaccess file in /new/ but in /app/webroot/new/.

Resources