Change displayed URL using htaccess - .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]

Related

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/

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]

My htaccess isn't working for a redirect permanent

I'm trying to do a permanent redirect with .htaccess, but it isn't working and I have no idea why.
RedirectPermanent / http://www.flunchinvite.fr
I'm trying to do a redirection from : http://www.flunchinvite.com to: http://www.flunchinvite.fr.
Do you have any ideas?
Thanks
edit
I've just did a test to do a redirect to google, and it doesn't work either, whereas when I try to do a redirect with the same code on http://flunchinvite.fr it works. Do you know where that can come from ?
Try something similar to
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^flunchinvite.com[nc]
RewriteRule ^(.*)$ http://www.flunchinvite.cfr/$1 [r=301,nc]
Use Rewrite if it is an option:
http://www.gnc-web-creations.com/301-redirect.htm
Another method we can use is via mod_rewrite. This requires that the
mod_rewrite module is active on your webserver. It usually is and is
done by the system administrators when they installed the webserver.
mod_rewrite is a very powerful URL re-writing engine and we will only
by scratching a hair on its head here.
Again, in your .htaccess file
RewriteEngine ON RewriteRule ^(.*)$ http://mynewdomain.com/$1
[R=301,L]
The above example will re-map your old domain to a new one and issue a
301 status code (permanent redirect). So a request for
http://olddomain.com/foobar.html will go to
http://mynewdomain.com/foobar.html
If you simply want to redirect all requests regardless of the page
requested to the new domain you could use:
RewriteRule /.* http://mynewdomain.com/ [R=301,L]
In this case no matter what file or directory is requested they will
all go to
http://mynewdomain.com/ i.e., http://myolddomain.com/foobar.html
will go to http://mynewdomain.com/
The [R=301,L] means redirect the client and send a 301 status code
(R=301) and make this the last rule (L).
At the end I did a php redirection, I don't know why it's not ok on the htaccess. I'll see that another time. I'm going to bed
Take a look at lines 5 and 6:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo2
RewriteCond %{HTTP_HOST} ^mathpdq\.com
RewriteRule ^(.*)$ http://www.mathpdq.com/demo2/$1 [R=permanent,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I could not get 301 redirects to work so I went with this. basically if the user goes in with mathpdq.com/demo2 it forces a redirect to www.mathpdq.com/demo2.
The stuff below line 6 is just the normal mapping into the php functions.
http://pastie.org/5364605

.htaccess URL rewrite does not work

I've wrote .htaccess file to convert urls to SEO friendly :
the original url is :
http://palestinianz.com/?page=person&p=Alex-Atalla
the content of .htaccess is :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
it should produce a link like this :
http://palestinianz.com/Alex-Atalla.html
But it makes no effect although I put the file in the root of my website !
where is the problem ?
If you want the URL to change in the address bar, you need to redirect the browser, then rewrite on the server. The first part of this answer explains the process that you want: https://stackoverflow.com/a/11711948/851273
So looking at the second list, the process is to redirect the browser if an ugly link is request to a nice looking link:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=person&p=([^\ ]+)
RewriteRule ^$ /%1.html? [R=301,L]
Now, on the server end you need to internall rewrite it back to the ugly url, which is more or less what you already have:
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
You can try:
RewriteCond %{REQUEST_URI} (.+)\.html$
RewriteRule ^(.+)\.html$ ./index.php?page=person&p=$1 [L]
Only problem with this is any .html page will go through this. May want to change to the following:
# URL: domain.com/person/Alex-Atalla.html
RewriteCond %{REQUEST_URI} (.+)/(.+)\.html$
RewriteRule ^(.+)/(.+)\.html$ ./index.php?page=$1&p=$2 [L]
This will allow you to have more robustness in changing the the page variable as well

Resources