Htaccess Pretty URL prevent access to original directory (duplicate pages) - .htaccess

EXPAINATION
In my htaccess file I have pretty urls set up, such as:
RewriteRule ^pricing$ /pages/pricing.php [L]
It works as expected, when the user goes to the following url:
http://www.example.com/pricing
It also works when the user goes to:
http://www.example.com/pages/pricing.php
QUESTION
My question is how can I prevent the user from being able to see the site at:
http://www.example.com/pages/pricing.php
A 301 redirect only creates a redirect loop.
Any suggestions?

Related

Redirect a Rewrite URL Address

Adding this to my .htaccess file works to shorten the URL and get rid of the .shtml bit.
RewriteEngine On
RewriteRule about about.shtml
But, the original address http://xxx/about.shtml also still exists. I don't want Google or anyone to see that one, so I tried redirecting it to the rewritten URL:
Redirect 301 /about.shtml /about
This gives an error on Firefox that it's not working and on Google that it is being redirected too many times. Maybe it's going in some sort of a loop.
I don't want the original file to show up anywhere in the web address (with the.shtml ending), so not sure what else besides a redirect to do.
Thanks!

.htaccess rewrite with subdirectory

I want to rewrite (not redirect) all url's of my site to a sub-directory of my site. For example:
http://example.com/example
would load the following:
http://example.com/public/example
Though, requesting http://example.com/public should not load the contents from public/public but from /.
Answers I've found on SO either do the above with redirect (which I don't want) or doesn't account for the special case above.
EDIT: further clarification:
I want every request on my site to go load under the public folder, but without being visible to the visitor. So requesting http://example.com/index.php will load the file from http://example.com/public/index.php. The url in the browser remains unchanged for the user.
Try the following rule :
RewriteEngine on
RewriteRule ^((?!public).+)$ /public/$1 [NC,L]
This will rewrite all requests from root to /public dir.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

ideas for a 301 redirect wildcards?

I'm having some trouble in finding an answer for a 301 redirect problem. The story goes like this:
I used to have a German language news site at www.punkto.ro. Every new article created with the site's CMS had the form: punkto.ro/articles/title-1234.html, where the 1234 is replaced by the article's number in the database.
For several reasons I had to redesign and opted for wordpress, which I placed in the root. I created a subdomain archive.punkto.ro and replaced the url variable "punkto.ro" in a config.php file with "archive.punkto.ro". Then I moved the site files to the subdirectory "archive". It works fine.
Of course, old article links in google now lead to a 404 response. To be sure, I also indicated the location of the archives on the 404, so people can go look there.
Now to the redirect: what I want to achieve is that when a user clicks a link in google punkto.ro/articles/title-1234.html, he/she should be redirected to archive.punkto.ro/articles/title-1234.html.
The strange stuff is that I can't find the folder "articles" in my file manager (cpanel OR ftp)... Does anybbody have any idea as to how the .htacces should look like?
Examples:
Old link: http://www.punkto.ro/articles/Staatschef_Basescu:_Rumaenien_einschliesslich_gesetzmaessig_fuer_Grexit_gewappnet-4169.html
New link: http://www.archive.punkto.ro/articles/Staatschef_Basescu:_Rumaenien_einschliesslich_gesetzmaessig_fuer_Grexit_gewappnet-4169.html
I don't think you have to create the .htacccess-file in the "articles"-folder. Just create it in the main folder.
I hope this will work for you:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?punkto.ro$
RewriteRule ^articles/(.*)$ http://www.archive.punkto.ro/articles/$1 [L,NC,R=301]
The RewriteCond makes sure that www.archive.punkto.ro will not be redirected again, the RewriteCond redirects every URL containing "articles/" to the archiv.

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

Resources