Remove directory in URL [HTACCESS] - .htaccess

I'm using this in my .htaccess file...
Redirect /index.html http://jurrasicgames.net/JurrasicGamesDatFile/Index.html
Redirect /VIP.html http://jurrasicgames.net/JurrasicGamesDatFile/VIP.html
Redirect /sg.html http://jurrasicgames.net/Soon.SHMTL
Redirect /kitpvp.html http://jurrasicgames.net/JurrasicGamesDatFile/kitpvp.html
I want http://jurrasicgames.net/JurrasicGamesDatFile/kitpvp.html to look like http://jurrasicgames.net/KitPVP is there any way to do this?

Don't use Redirect directive as it does a full redirect. Use internal rewrite instead.
.htaccess to be used in site root ( a level above JurrasicGamesDatFile )
RewriteEngine On
RewriteRule ^index\.html$ JurrasicGamesDatFile/Index.html [L,NC]
RewriteRule ^VIP\.html$ JurrasicGamesDatFile/VIP.html [L,NC]
RewriteRule ^sg\.html$ Soon.SHMTL [L,NC]
RewriteRule ^kitpvp\.html$ JurrasicGamesDatFile/kitpvp.html [L,NC]
Make sure to clear your browser cache before testing it.

Related

URL Rewriting with .htaccess redirects

i am trying to Generate
http://example.com/file/1.php?name=the-file-name-any&id=32vr&code=1548393
to
http://example.com/file/the-file-name-any.32vr/1548393
but i am not success
my .htaccess redirect code
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /file/1.php?the-file-name-any=$1&id=$2&code=$3 [L]
Try using this rule:
RewriteEngine On
RewriteRule ^file/([^/]*)/([^/]*)/([^/]*)$ /file/1.php?name=$1&id=$2&code=$3 [L]
Make sure it is at the top of your .htaccess and you've cleared your cache before testing.

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]

How is it possible that redirect works, even after applied rule in htaccess?

I have a wordpress site with an htaccess that looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
And then after I have the following redirect:
Redirect 301 /plans /samples
And this is doing the redirect correctly.
How is that even possible? If it is declared after the conditions that say that if the file or directory does not exist, let's index.php handle the request? It shouldn't even get to that redirect, right?
Well, the Rewrite**** directives all belong to the mod_rewrite module, while the Redirect directive belongs to the mod_alias module. Both modules are in the URL mapping processing pipeline so both sets of directives get applied.
If you want the redirect to work in-line with the rewrite rules, then you shouldn't use mod_alias, use only mod_rewrite instead:
RewriteRule ^plans(.*)$ /samples$1 [L,R=301]
is the mod_rewrite equivalent of your Redirect.

.htaccess to partially redirect index requests

I'm doing some work for a friend on their website, and while we are working on the new site I would like to set up their .htaccess file to redirect users with the following rules:
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
This is probably a simple request, but I have no experience with the rules language that .htaccess uses.
Thanks in advance!!
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
RewriteEngine On
RewriteRule ^(index\.html)?$ /legacy/index.html [L]
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
RewriteRule ^index\.php$ - [L]
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
RewriteRule ^beta/?$ /index.php [L]
These would all go in the htaccess file in your document root. If you actually wanted to redirect the browser so that the URL in the address bar changes, include a R=301 in the square brackets: [L,R=301]. If when you say "requests to a second subdirectory get redirected to index.php" meaning "any subdirectory", then the rule needs to be changed to:
RewriteRule ^([^/]+)/?$ /index.php [L]
i kind of did the reverse with codeigniter ...
RewriteEngine On
RewriteBase /myproject
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$config['base_url'] = 'http://localhost/myproject/';
$config['index_page'] = '';
hope this helps

Redirect 301 & Mod Rewrite Issues

I have a online store that, for example, is located here: hxxp://domain.com/store/
Within the store directory's .htaccess file I have this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /store/
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/store/$1 [R=301]
RewriteRule ^/?$ directory.php
RewriteRule ^search/?$ search.php
RewriteRule ^category/([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]
It works great!
My issue (problem) is that I now need to change the /store/ directory to /shop/ which seemed simple enough. However, I need to setup proper 301 redirects so that I don't loose any SE rankings that I may have.
What is the best way to setup 301 redirects in this situation?
The only solution I have found is to setup a redirect 301 for each category, product etc. in the store. Like so, for example.
Redirect 301 /store/category/sample-widgets/ hxxp://www.domain.com/shop/category/sample-widgets/
This works and does what I need it to, but... the URL in the address bar displays like so: hxxp://www.domain.com/shop/category/sample-widgets/?CategoryID=sample-widgets
I can not figure out why or how to remove the query string.
Please help. Thanks.
You can handle the 301 errors by using a PHP script to handle the redirects.
In your .htaccess file you would add this rule:
Redirect 301 /error301.php
Create the file error301.php:
<?php
$redirects = array('/path/to/old/page/' => '/path/to/new/page/',
'/store/category/sample-widgets/' => '/shop/category/sample-widgets/');
if (array_key_exists($_SERVER['REQUEST_URI'], $redirects))
{
$dest = 'http://'.$_SERVER['HTTP_HOST'].$redirects[$_SERVER['REQUEST_URI']];
header("Location: {$dest}", TRUE, 301); // 301 Moved Permanently
}
You could use a simple Redirect directive like:
Redirect 301 /store/ /shop/
Or, if you want to use mod_rewrite as well, you would need to change your current rules as you can not use the base URL /store/ anymore:
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^store/?([^/].*)?$ /shop/$1 [L,R=301]
RewriteRule ^shop/?$ directory.php
RewriteRule ^shop/search/?$ shop/search.php
RewriteRule ^shop/category/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^shop/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]

Resources