Rewrite, Redirect, and avoid duplicate content - .htaccess

I'm trying to redirect a single page using .htaccess. The actual file is /abc.php, I want it to appear as /abc. So these are my pseudo-rules:
if the user hits /abc.php, rewrite the url as /abc
if the user hits /abc, serve /abc.php
For SEO purposes, only one of /abc and /abc.php should be available.
My naive approach is this:
Redirect 301 /abc.php /abc
RewriteRule ^abcs$ /abc.php [NC,L]
This is causing infinite, looping redirects. How do I do this correctly?

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(abc)\.php[\s?] [NC]
RewriteRule ^ %1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^(abc)/?$ $1.php [L,NC]

Related

RewriteEngine - forcing new friendly URLs causes too many redirections

This rule translates URLs like
http://www.example.com/index.php?page=about to friendly URLs like http://www.example.com/about
RewriteRule ^([a-zA-Z_]+)?$ index.php?page=$1 [NC,L]
It's working well. But, currently old URLs (like http://www.example.com/index.php?page=about) are indexed in google. So, in addition to that rule I need to redirect all old URLs (like http://www.example.com/index.php?page=about) to new friendly URLs (like http://www,example.com/about) so when visitor old URL he gets redirected to friendly URL. I wrote that rule:
RewriteCond %{QUERY_STRING} ^page=([^&]+)$ [NC]
RewriteRule ^index\.php$ http://www.example.pl/%1? [NC,R=301,L]
and it redirects to new URL but instead of showing a website content, it throws an error ERR_TOO_MANY_REDIRECTS.
Any help?
You are getting too many redirect error because of your internal rewriteRules,as they internaly forward the request to the same location after first rewrite iteration, to fix this , add the following at top of your htaccess,
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
or you can match against %{THE_REQUEST} instead of %{QUERY_STRING} to avoid rewrite loop/too many redirect error
RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s]+) [NC]

Remove /views/ from url with rewrite

I'm trying to learn how to use htaccess rewrite, but I just can't get the hang of it. What I'm trying to do is make the following urls:
mysite.com/
mysite.com/views/clients
mysite.com/views/projects
mysite.com/views/estimates
...look like this:
mysite.com/
mysite.com/clients
mysite.com/projects
mysite.com/estimates
It should be simple enough. But I just can't make it work.
Assuming the URLs in your app are already of the form /clients, not /views/clients, then try the following in the .htaccess file in your document root:
RewriteEngine On
# Remove "views" from all (typed) URLs with external redirect
RewriteCond %{THE_REQUEST} /views/([^\ ]*)
RewriteRule .* /%1 [R,L]
# Internally rewrite specific requests back to /views
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(clients|projects|estimates)/(.*) /views/$1/$2 [L]
Once you are satisfied this is working then change the R (temporary) redirect to R=301 (permanent).
This checks against THE_REQUEST before the redirect in order to prevent a redirect loop.

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 mod_rewrite and 301 redirect to friendly url

Now, I'm trying to write some htaccess code with mod_rewrite. I have problem with it :(
RewriteEngine On
RewriteRule ^home$ index.php?page=home
This is my code in .htaccess file. When I go to domena.com/index.php?page=home it's exactly same like domena.com/home
But, It's not friendly for google, 'cos we have double page:
domena.com/index.php?page=home
domena.com/home
It's same.
What I want to achieve? I want to user who choose domena.com/index.php?page=home redirect him to domena.com/home
Exactly, I want to on my website be exist ONLY friendly link.
Help me :(
You will need another rule to redirect old URL to new one.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteRule ^home/?$ index.php?page=home [L,NC,QSA]

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