Hide the path in website URL using PHP - .htaccess

I have no idea of URL coding, please help me out.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2

If you want to do this from php you can use a rewrite rule from your .htaccess file to pass everything to index.php
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
And after that you can get $_SERVER['REQUEST_URI'] that contains the current path (if you want to do something with it) and redirect back to your domain.
header("Location: http://$_SERVER[HTTP_HOST]");

Related

htaccess removing folder from url externally and redirecting internally

I manged to remove the folder from the url but I cannot redirect back to it. Please help!
# externally redirect /dir/foo to /foo
RewriteCond %{THE_REQUEST} pages/
RewriteRule pages/(.*)$ $1 [L,NC,R]
# internally forward /foo to /dir/foo
#what goes here?
EDIT:
Also, how can I put this on the same code?
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ profile.php?username=$1 [L]
I want the user name to be like this localhost/test/user1 rather than localhost/test/profile.php?username=user1
Internal redirect mean URL rewriting (if I'm not misunderstood)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /dir/$1 [L]
Note: To prevent external redirect I didn't used [R] flag.
EDIT
I can see you hostead on localhost in subdirectory /test So, you'l have to set RewriteBase as following:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /test/dir/$1 [L]
EDIT Included user profile rewrite rule
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1 [L]
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /test/dir/$1 [L]

UrL Rewrite using code with my domain

URL : http://domainname.com/index.php?p=top-games
I need to rewrite this as http://domainname.com/top-games
How I do this using htaccess file? Please can any one give me the htaccess code.
Thanks
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule
Create .htaccess file in your root folder and paste code
You're probably looking for redirect in reverse direction.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?q=$1 [L,QSA]

.htaccess 301 Redirect non slash domains and slash domain to .html

I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html

How to redirect undefined RewriteRule in .htaccess

Here's what I've currently written in my .htaccess file:
RewriteEngine on
RewriteRule ^contact contact.php
RewriteRule ^browse browse.php
RewriteRule ^contact/ contact.php
RewriteRule ^privacy privacy-policy.php
RewriteRule ^signup register.php
RewriteRule ^register register.php
So when a user requests http://mydomain.com/welcome, which is not defined in the .htaccess file, it will result in a "404 Not Found" page.
Instead, I'd like to redirect pages that aren't explicitly defined in the .htaccess to another script, and pass the original URL. I don't want to use ErrorDocuments here. How can I do this?
RewriteEngine on
RewriteRule ^contact contact.php
RewriteRule ^browse browse.php
# why is contact the only one with a slash at the end?
RewriteRule ^contact/ contact.php
RewriteRule ^privacy privacy-policy.php
RewriteRule ^signup register.php
RewriteRule ^register register.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /other.php [L]

.htaccess redirect from 1 url (no matter what file/folder) to another url

I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]

Resources