Redirect generic page to specific - .htaccess

Im trying to redirect any page that starts with the directory blog to a specific page.
Here is what I what to do:
test.com/blog/ => test.com/news/
test.com/blog/anything => test.com/news/
Its working well with the root blog/, but it does work well for the articles.
What should I do?
RewriteEngine On
Redirect 301 ^/blog/?$ https://test.com/news$

RewriteEngine and Redirect are directives from 2 different module. Don't mix up those. Moreover you don't need $ in target as regular expression is only used for matching a pattern.
You can use following rule inside the /blog/.htaccess file:
RewriteEngine On
RewriteRule ^ /news/ [L,R=301]
This will redirect any request that starts with /blog/ to /news/.

Related

Redirect and keep the parameter in the url on .htaccess

It should probably be an easy setup, but without familiarity with the terms used, I can't come up with a solution. I tried several examples that I found on the internet.
I just wanted that when the user accessed the site via a url https://example.com?foo=bar he would be directed to https://www.example.com/new-nice-page?foo=bar
Changing the page and keeping the parameter.
The last thing I tested:
RewriteEngine on
RewriteRule ^https://www.example.com/new-nice-page?foo=$ example.com?foo=$1 [QSA]
You may use this redirect rule at the top of .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)foo= [NC]
RewriteRule ^/?$ /new-nice-page [L,R=301]
Note that query string is automatically carried forward to target URL.
Looking at this thread : simple .htaccess redirect : how to redirect with parameters?
you simply redirect , without worying about the parameters, because they are automatically passed :
Redirect permanent /jsn.php http://www.site2.com/jsn.php

Rewrite rules for assets directory

I am trying to do the following:
Redirect https://www.example.com/website to https://www.example.com/website2020
Get images to show up on my page. When viewing the page, I have broken images that are not showing up as it is looking for images located: https://www.example.com/website/assets/
The images have moved to https://www.example.com/website2020/assets/
### Redirect seems to be working
RedirectMatch 302 (?i)^/website(/.*)?$ /website2020
### Rewrite rule not working
RewriteRule ^/website/assets(/.*)?$ /website2020/assets
How would I rewrite so that it shows the images when viewing the webpage?
Just try a simple rule like this:
RedirectMatch 302 (?i)^/website(/.*)?$ /website2020$1
$1 is back-reference of capture group #1 i.e.(/.*).
Answering in reverse...
Get images to show up on my page. When viewing the page, I have broken images that are not showing up as it is looking for images located: https://www.example.com/website/assets/
If the images have moved (internally) and you are not able to modify your HTML source code then you should consider implementing an internal rewrite for your assets, not an external redirect. An external redirect will be hopelessly inefficient, particularly if you have many images. (An external redirect can still be used for the main URL - see below.)
### Rewrite rule not working
RewriteRule ^/website/assets(/.*)?$ /website2020/assets
In .htaccess the URL-path matched by the RewriteRule pattern does not start with a slash. You are also failing to copy the requested "asset" (image). If this only applies to images (as you suggest) then consider only matching images.
For example, try this at the top of your .htaccess file:
RewriteEngine On
# Internally rewrite assets
RewriteRule ^website/assets(/.*)?$ /website2020/assets$1 [L]
The L flag is required if you have other mod_rewrite directives later in the file.
Redirect https://www.example.com/website to https://www.example.com/website2020
If you have implemented an internal rewrite as above then you should implement the redirect using mod_rewrite, not a mod_alias RedirectMatch (which executes later).
For example:
RewriteEngine On
# Redirect everything, except assets
RewriteCond %{REQUEST_URI} !^/website/assets
RewriteRule ^website(/.*)?$ /website2020$1 [R=302,L]
# Internally rewrite assets
RewriteRule ^website/assets(/.*)?$ /website2020/assets$1 [L]

Redirect all urls which contain certain parameters to another url which follows a certain pattern

Unfortunately I didn't get it solved by myself and need to ask for help. I want to redirect all urls which follow a certain pattern (in this case it contains "reviews/category"). These URLs supposed to be redirect to another url which is made up the first one:
http://[product-url]/reviews/category/[category-url]
supposed to be redirect to
http://[product-url].html
Furthermore it shouldn't matter if you call the url with or without www.
Example:
http://example.com/ford-blues/reviews/category/cars supposed to be redirect to http://example.com/ford-blues.html
Any help would be much appreciated.
Following code isn't working
RewriteEngine On
RewriteRule ^reviews/category/?$ $1\.html [R=301,L]
Try:
RedirectMatch 301 ^(.*)/reviews/category/ /$1.html
in the htaccess file in your document root.
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*)/reviews/category/ /$1.html [L,R=301]

Redirect Joomla core pages via .htaccess

I'm having trouble redirecting Joomla's core pages via .htaccess; for instance, I need
/component/users/?view=login
to redirect to another page, but the server seems to ignore the redirect entirely. Is there something I'm missing here? Currently, I'm trying to use:
Redirect /component/users/?view=login http://www.example.com/
You can't match against the query string in a Redirect, you need to use the %{QUERY_STRING} variable and mod_rewrite. Above any other rules you may already have in the htaccess file in your document root, add:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)view=login(&|$)
RewriteRule ^component/users/$ http://www.example.com/? [L,R]

.htaccess rewrite rules that include a sub-folder

Here is what I'm trying to do.
I have 2 apps running on my site one at root / and one at at /blog/.
I want to be able to access some pages that are served by the blog app but without the /blog/ part in the URL. This bit I have managed to do with the following rule.
RewriteRule my-page blog/index\.php?page_id=1 [L]
This allows me to view a page actually at /blog/my-page at /my-page.
Now where I'm struggling is with ensuring there are no duplicate URLs, so I'm trying to redirect to my shorter URL. Like so:
From:
/blog/my-page
To:
/my-page
From:
/blog/index.php?page_id=1
To:
/my-page
With the following rule, I can redirect from /index.php?page_id=1 to /my-page
RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule index\.php$ my-page? [R=301,L]
After this rule happens the first rule I mentioned takes it to the right place.
My question is:
How can I get it to work with blog as part of the URL. I expected the below to work but it doesn't
#RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule ^blog/index\.php$ my-page? [R=301,L]
The reason this was not working was because there was an .htaccess file within my /blog/ directory.
This included a rule that would catch anything and route to blog/index.php.
Moving my rules to /blog/.htaccess allowed them to function properly

Resources