HTACCESS to Remove /amp/ from the end of a URL - .htaccess

/amp/ was showing up at the end of all of my URLS incorrectly. I have fixed it, but I now have a mess. I have many urls with /amp/ on search that I need to redirect to a url with the /amp/ removed from the end. How would I do this with .htaccess?
I tried this:
RewriteEngine On
RewriteRule ^(.*)/amp/?$ /$1/ [L,R=301]
And it does not seem to work.

If you are using Apache Web Server, you can use below rule for redirecting your all AMP urls to NON-AMP version.
Example
https://example.com/permalink/amp/ to https://example.com/permalink/
# Redirect from AMP to non-AMP path
RewriteEngine On
RewriteCond %{REQUEST_URI} (.+)/amp(.*)$
RewriteRule ^ %1/ [R=301,L]
I hope this helps.

Related

How can I redirect old URLs to new URLs using htaccess? [duplicate]

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

Change displayed URL using htaccess

How could I rewrite my URL using HTACCESS?
When a visitor visits index.php, I want the URL to be rewritten to something else, although the visitor will remain on the index.php page.
This is what I've tried (I did research this before asking but couldn't solve it myself):
RewriteEngine on
RewriteRule ^index.php$ testingit.php
Basically, I just wanted to change index.php to 'testingit.php', just to see if it would work.
You can use this code in your testwebsite/.htaccess file:
RewriteEngine On
RewriteBase /testwebsite/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^ home [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^home/?$ index.php [L,NC]
So here's how you do it:
redirect *** /index.php http://www.yoursite.com/testingit.php
You need to replace *** with one of the following:
301-For a permanent redirect meaning browsers update bookmarks etc.
or
302-For a temporary redirect
Here's a link to a guide I found:
https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/
Hope this helps :)
To make pretty URL's:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^home/?$ index.php [NC,L]

htaccess Re-writing URL with redirect causes loop

I have an existing site with php in the URL indexed. I want to change the URLs to not include PHP so thepage.php will be rewritten to http://example.com/thepage
The rewrite is working, but the redirect causes a 'page can't be displayed in a loop' error when I try to access the page as using thepage.php
I need to figure out how to setup the redirect and also rewrite the URLs, because Google has now indexed the PHP URLs and the pretty URLs.
I've looked at countless pages online, but can't find the answer.
Please help.
Here's my htaccess
# The redirect below caused a loop when I access the page as
# http://example.com/thepage.php
# Moving the redirect after the rewrite didn't work either.
Redirect 301 /thepage.php http://example.com/thepage
RewriteEngine on
# Rewrite works. The page does display corectly for this link
# http://example.com/thepage
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
# RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
#RewriteRule ^photos([^\.]+)$ photos [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
ErrorDocument 404 http://example.com/
Thank You
Your rewrite rule is backward from the sounds of it.
Here we're capturing all php files that do no contain periods and redirecting them to a directory URI using only the name without the extension. [R=301] is a permanent 301 redirect that tells whoever that the page is no longer in its old place.
RewriteEngine on
RewriteRule ^([^\.]+).php$ $1 [NC,L,R=301]
You might want to test these with R only without the 301 to make sure it's working. If you do a 301 redirect that doesn't work it will be cached in the end user's browser cache until it expires.
Apache has an extensive reference on remapping URLs with redirects.
I finally was able to get back to this problem and found the solution. I hope this can help somebody else. I tore my hair out on this problem.
I found many examples but not with the combination that I needed
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
RewriteBase /
# hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
ErrorDocument 404 http://example.com/

Redirect to another website using wordpress and querystring

I am new to WordPress and want to redirect some specific traffic. Here's an example -
A user from a different website clicks on a link with url http://mywordpresssite.com/redirect?url=http://amazon.in/blablabla
I want my website to redirect the request to
http://amazon.in/blablabla or whatever be the url in the querystring without loading anypage of my website.
My website should function normally for all other requests.
I tried with htaccess and came up with this -
RewriteCond %{QUERY_STRING} (?:^|&)url=([^&]+)(?:&|$) [NC]
RewriteRule ^(.*)$ %1 [R=302,L]
But the RewriteRule results in http://mywordpresssite.com/http://amazon.in/blablabla?url=http://amazon.in/blablabla which is wrong.
I am new to htaccess and would be grateful for any help.
Try the following in .htaccess in the root of your site. Note that this should go before your existing WordPress mod_rewrite rules but after RewriteEngine On.
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^redirect$ %1 [R=302,QSD,L]
The %1 refers to the first parenthesised subpattern in the RewriteCond directive (ie. everything after the "url=").
The QSD flag (Apache 2.4+) removes the query string from the original request.
Before Apache 2.4 you would need to change the RewriteRule to read (note the additional ? at the end of the substitution):
RewriteRule ^redirect$ %1? [R=302,L]

How to redirect a page without the page extension

I am currently working on a signup page and I was wondering if I could modify the URL without the .php extension.
For example, it is now
www.xyz.com/signup.php
And what I would like achieve is
www.xyz.com/signup
Now I am assuming that I might have to use the htaccess file, but I am not sure about it.
First rule will redirect from signup.php to domain.com/signup/.
Second rule will redirect internally so the URL will remain domain.com/signup/ while displaying the content of domain.com/signup.php.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /signup.php to /signup/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+signup\.php\s [NC]
RewriteRule ^ /signup/? [R=302,L]
# Internally forward /signup/ to /signup.php
RewriteRule ^signup/?$ /signup.php [NC,L]
What you're looking for is called URL rewrite. Take a look at this tutorial for beginners: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/.
You can duplicate with .htaccess
RewriteEngine On
RewriteRule singup$ singup.php [NC,L]

Resources