htaccess rewrite - Hide subfolder from URL - .htaccess

We have a page file here:
ourdomain.com/subfolder1/subfolder2/
But we'd like people to be able to access it by going here:
ourdomain.com/subfolder2/
So we need both a redirect and a rewrite.
Any idea how this can be achieved using htaccess? I've tried a million things and nothing is properly rewriting/redirecting.
What I currently have...
RewriteCond %{REQUEST_URI} ^/subfolder1/subfolder2/?$ [NC,OR]
RewriteRule ^(.*) http://www.ourdomain.com/subfolder2/ [R=301,L]

You can use:
# Redirect from /subfolder1/subfolder2/...
RewriteCond %{THE_REQUEST} \s/+subfolder1/(subfolder2/[^\s?]*) [NC]
RewriteRule ^ %1 [R=301,L]
# Rewrite to /subfolder1/subfolder2/...
RewriteRule ^(subfolder2/.*) /subfolder1/$1 [NC,L]

Related

Redirect dirty urls to a clean url

Alright so I have done the process of cleaning up my urls and everything is going good I put the following in my htaccess.
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /l/index.php?mp3=$1&img=http://www.dailynewsounds.com/wp-content/uploads/2016/01/tinashe-5535799554a1b-3.jpg&g=R\%26amp\%3BB&p=8\%20hours\%20ago&s=+Energy&a=Tinashe+Ft.+Juicy+J+ [L]
And this works to change my urls but we have our nasty urls directing to the pages.
http://dl.example.com/l/index.php?mp3=oo8aey7qw3n9&img=http://www.example.com/wp-content/uploads/2016/01/stuey-rock-ft-sy-ari-da-kid-569e83c69cf5e.jpeg&g=R%26amp%3BB&p=10%20hours%20ago&s=+Me+And+You&a=Stuey+Rock+Ft.+Sy+Ari+Da+Kid+
How can I make it so that I can check my url to see if it contains index.php and if it does redirect it to the new page?
Try the following rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /l/index\.php\?\S*mp3=\S* [NC]
RewriteCond %{QUERY_STRING} \bmp3=([^\s&]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
If you're using Apache 2.4.x, then replace the L] with L,QSD].

Redirect URLs from Wordpress with ?post= or ?cat= in htaccess

So I need to redirect a mass of old Wordpress short urls that begin with question marks such as:
/?post=731
Or
/?cat=73
I need them to be instead /page
I tried
RewriteRule /?cat=73$ http://www.mydomain.com/page? [R=301,L]
Didn't work
I tried
RewriteCond %{QUERY_STRING} ^cat=73$
RewriteRule ^/$ page? [L,R=301]
Didn't work.
Each old URL will have to be manually redirected to a new url. Meaning:
/?cat=73 goes to /category-73
/?cat=2 goes to /main-category
/?post=731 goes to /this-page
These are just examples.
Each old link will have a NEW link - no one size fits all.
I have about 500 URLs like this. Anyone got any good ideas?
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^cat=73$ [NC]
RewriteRule ^ /category-73? [L,R=301]
RewriteCond %{QUERY_STRING} ^cat=2$ [NC]
RewriteRule ^ /main-category? [L,R=301]

Please take a look at this .htaccess file

I am trying to implement 301's for sub-pages on my site which is hosted on an Apache server (Fasthosts), using Rewrite scripts in the .htaccess file. I have tried to follow many pieces of documentation (infact, I've never had any problems implementing 301's using .htaccess before!) but on this particular website, nothing seems to work.
There is currently a 301 re-direct in there from non-www to www which is working fine. There are also some other snippets using regex which I imagine are for the CMS.
Below is the current state of the .htaccess file. An example of a 301 I'm trying to re-direct is in there (Lines 5 & 6)
Old page: http://www.junkwize.com/home-Garden%20Clearance
to New page: http://www.junkwize.com/services/garden-clearance-london
.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^junkwize.com
RewriteRule (.*) http://www.junkwize.com/$1 [R=301]
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
#Options +FollowSymlinks
RewriteRule ^.htaccess$ — [F]
RewriteRule ^([/admin]+)$ admin/login.php [L]
RewriteRule ^([/admin]+)([/blocks]+)$ admin/login.php [L]
RewriteRule ^([/blocks]+)$ index.php [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)-([^/\.]+)$ index.php?main=$1&id=$2&menu=$3 [L]
RewriteRule ^([/deals]+)-([^/\.]+)$ index.php?main=$1&id=$2 [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)$ index.php?main=$1 [L]
Any help would be much appreciated.
Many thanks.
This code is faulty and is not going to work:
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
Reason is that RewriteCond %{HTTP_HOST} can only match host name. Replace that code with this:
RewriteRule ^home-Garden\ Clearance/?$ /services/garden-clearance-london [R=301,L,NC]

Rewrite URLs with .htaccess

I am trying to rewrite my URLs so they are search engine friendly.
Example URL is:
http://www.example.com/?Products=Displays&SubCat=Chalkboards-and-Accessories&Item=Budget-Framed-Chalkboards
I am thinking maybe something like this:
http://www.example.com/Chalkboards-and-Accessories/Budget-Framed-Chalkboards
Instead of using {Domain}/Chalkboards-and-Accessories/Budget-Framed-Chalkboards URL,
I'm suggesting you should use {Domain}/Products/Chalkboards-and-Accessories/Cafe-and-Rope-Barrier-Systems, because in your site you are having URLs like {Domain}/?About.
Try this,
RewriteEngine On
RewriteCond %{HTTP_HOST} !^catalogue\.signalight\.co\.uk$
RewriteCond %{HTTP_HOST} !^blog\.signalight\.co\.uk$
#RewriteCond %{REQUEST_URI} !^/catalogue
#RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^([a-zA-Z0-9-_]+)/?$ ?$1 [L]
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/?$ ?$1=$2 [L]
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/?$ ?$1=$2&SubCat=$3 [L]
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)?$ ?$1=$2&SubCat=$3&Item=$4 [L]
this is a nice artical for basics url rewriting.

htaccess rewrite still some problems, getting a bit complex eek!

Another edition to my .htaccess rewriting saga. Everything is now working to a degree but still have some issues.
I have numerous rules to move old urls to new urls, passing variables etc - however there are still a couple of things i need to add, and for the life of me cannot figure out.
I have 3 urls that are directing/rewriting as below.
1 www.mydomain.com/news/dentistry_dental/index.php
2 www.mydomain.com/news/dentistry_dental/index.php?month=April&year=2011
3 www.mydomain.com/news/dentistry_dental/article_detail.php?article=1234&title=some-title
These are redirected & rewritten perfectly to the new urls respectively
1 www.mydomain.com/dental_news/
2 www.mydomain.com/dental_news/April-2011
3 www.mydomain.com/dental_news/1234-some-title
However... Here is the problem #1 The following urls are also redirecting as below
4 www.mydomain.com/news/it_technology/index.php?month=April&year=2011
5 www.mydomain.com/news/it_technology/article_detail.php?article=1234&title=some-title
Which are also directing to the same urls as the dental redirects
4 www.mydomain.com/dental_news/April-2011
5 www.mydomain.com/dental_news/1234-some-title
Which shouldnt be happening. The it_technology news articles have now been removed so i wish to redirect them either to my homepage with a 410 or something similar, whichever is the best option really.
My current .htaccess looks as follows.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{REQUEST_URI} !/generator/
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
RewriteRule ^products/dental-digital-imaging/([^/]*)_([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
RewriteRule ^products/package_deals.php$ http://www.mydomain.com/products/dental-computer-network-bundles [R=301]
RewriteRule ^products/computer_hardware.php$ http://www.mydomain.com/products/dental-computer-solutions [R=301]
RewriteRule ^products/individual_computers.php$ http://www.mydomain.com/products/dental-computer-systems [R=301]
RewriteRule ^products/digital_xray_imaging.php$ http://www.mydomain.com/products/dental-digital-imaging [R=301]
RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]
# Rewrite dental news article to neat nice url
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([0-9]*)-([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([a-zA-Z]*)-([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]
# Rewrite URL stripping .php Extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
My only other requirement would be to add a trailing slash to the php files served ( that have allready add the .php extension removed with the last rule in my .htaccess.
I have tried numerous ways that i have found via google, but all of them cuase probelms with my other rules.
Hopefully someone can help me finish this off once and for all.
Regards
M
Sure is getting complicated :) Try changing these lines (I think there are 2):
RewriteCond %{REQUEST_URI} !^/dental_news/
to:
RewriteCond %{REQUEST_URI} ^/news/dentistry_dental/
That way its only going to redirect the old dental news URLs to there respective new ones.
Trivially, your problem is the catch-all rules:
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]
Restrict them to the path you want to rewrite, i.e. replace .* with something more specific, like:
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [R=301]
You can do the news/it_technology redirect as a standard redirect.
RewriteRule ^news/it_technology http://www.thepage.com/ [R=301,L]
It might technically be more correct to do a 410, but the 410 code is really optional. You might also want to make it 404, and then have a link from the 404 page to the main page.

Resources