Redirect .asp pages to home page of my new site - .htaccess

I changed my website from .asp pages to Joomla which uses .php. The page structure has not been maintained. Now, I would like to have all traffic coming in from the indexed .asp pages (by the search engines) redirected to the home page of my site.
I have used the following Rewrite rule
RewriteRule \.asp$ ^/index.php [R=301,L]
but the rule redirects to
https://example.com/home/example/public_html/index.php?key=1234
How can I simply remove the /home/example/public_html/ and also the ?key=1234 parameter while performing the redirect. Or simply saying, how can I just have the redirect go to the home page of my new site.
Thanks.

First see this picture from Apache below :
So , a substitution could not contains Regular Expression as you did here ^/index.php this ^ should be removed first then see what you want to do .
https://httpd.apache.org/docs/2.4/rewrite/intro.html
Replace your code by this :
RewriteRule \.asp$ http://example.com? [R=301,L]
I put ? after example.com to prevent query string like key=1234 to be appended in new target .
Note: clear browser cache then test it .

Related

redirection with .htaccess (removing subfolder from URL and keep them into website)

I just want to redirect this kind of URL :
mydomain.fr/public/pages/project.php
To this kind of URL
mydomain.fr/project.php
I'm trying this kind of stuff in my .htaccess file
Redirect /public/pages/ http://mydomain.fr/
OR
RewriteEngine On
RewriteRule ^public/(.*)$ /$1 [L,NC,R=302]
(to remove just the 'public' folder).
Its working so when I click on a link of my website, the URL is redirected to what I want, but it broke the navigation. So I can't access my page (error like : The requested URL was not found on this server.) occured.
My question : How do I remove these subfolder in my URL without removing these subfolder into the server of my website ? I dont know if i'm clear, sorry my english is not perfect.
Thanks !
Have a good day

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

Use of page to redirect

I need to set up a page to redirect from our old server to our new server, advising people of the new URL and to change their bookmarks. I know how to use the refresh meta tag in HTML to do this.
But I also want to set it up so that they would see the redirect page no matter what page they have navigated to on the old server. I see several solutions using 301 redirect.
How would I do both? We're running an Apache server on Debian server.
You need to use Apache an RewriteRule using mod_rewrite. This can be placed in an .htaccess file on your server’s root, or it can be placed directly into your Apache config file.
So let’s say your new redirect page—where you explain the site has moved and have set a refresh meta tag—is called redirect.php, then this is the Apache RewriteRule that should work for your needs.
RewriteEngine on
RewriteRule ^(.*)$ /redirect.php [L,R=301]
This will grab any URL on the site this RewriteRule is placed on & redirect them to /redirect.php. That /redirect.php can also be a full URL such as http://mysite.com/redirect.php or anything else.

redirect rule for multiple pages

I have page not found erros in webmaster because the page/2/0 part of a url is due to smart paging module clean url feature (Drupal) now i uninstalled the smart paging module but these page not found errors are still there.
www.mysite.com/a/b/c/page/2/0,
www.mysite.com/a/d/e/page/3/0,
www.mysite.com/a/f/g/page/4/0,
www.mysite.com/a/h/i/page/5/0
and so on.
I want to redirect
www.mysite.com/a/b/c/page/2/0 to www.mysite.com/a/b/c
www.mysite.com/a/d/e/page/3/0, to www.mysite.com/a/d/e
www.mysite.com/a/f/g/page/4/0, to www.mysite.com/a/f/g
www.mysite.com/a/h/i/page/5/0 to www.mysite.com/a/h/i
with one redirect rule. How to do this
in short i want to remove the page/x/0 part from the url and redirect it to the remaining part of that url.
You want something like this:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/[0-9]+/0$ /$1/$2/$3 [L,R=301]
The literals here are after the first 3 path nodes: /anything/anything/anything/, then it must be followed by a page, then some numbers, then a zero. You can make it even more general, say after the "page", by changing the pattern to:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/ /$1/$2/$3 [L,R=301]

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

Resources