how to hide filename using .htaccess , create custom username on url - php

I want remove file name from url .
my current url is:
http://www.demo.com/user.php?name=joon
and I want to :
http://www.demo.com/joon
if I use this code
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?name=$1
output is
http://www.demo.com/user/joon
but I want
http://www.demo.com/joon

Try this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /user.php?u=$1 [L]
RewriteCond is important here to avoid rewriting your existent directories to /user.php.
This will rewrite /user to /user.php?u=user .

You can achieve this using Apache URL Rewriting. You need to make changes to your .htaccess file. This blog post covers it nicely
https://expozit.wordpress.com/2012/07/25/apache-url-shortening/

Related

Setting redirect in .htaccess and keeping the paths

I need to set the redirect in htaccess.
My htaccess is given below
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . frontend/web/index.php
So it redirects all requests to frontend/web/index.php
But the problem is that Suppose the following request comes /site/about
Currently using the above rules frontend/web/index.php is not receiving the path parameters. ie. /site/about is redirecting to
I need the path parameters as path parameters itself in frontend/web/index.php
.ie. /site/about should be mapped to /frontend/web/index.php/site/about
Also the path portion is dynamic. ie it can be anything like site/service
How can I do that rule in htaccess. I have checked some other solutions but most of them says to map path parameters to query strings which I cannot do as per my site settings.
You'll need to append the original path to the new one:
RewriteRule (.*) frontend/web/index.php/$1
As #Rickdenhaan said, You need to append the request path to your destination url. You can appened it using %{REQUEST_URI} variable or using a backrefrence at the end of your rewrite target.
Change your RewriteRule line to this :
RewriteRule . frontend/web/index.php%{REQUEST_URI} [L]

Hiding controller and method from the URL but show only title name

I looked at the suggested answer(possible duplicate as stakoverflow says). It provided help but didn't solve the problem as it won't help create clean URL as per my needs. It has controller as part of the URL which I don't want to see as mentioned below.
I am developing a tutorial site in Codeigniter, and looking for a way to generate SEO friendly URL like
www.crdlabs.com/how-to-use-multidimensional-arrays
instead of
www.crdlabs.com/home/index/how-to-use-multidimensional-arrays.
Can anyone share his ideas? Thank you
First you need to hide the index.php from URL so create the .htaccess file on the root folder and add this code
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Then open the routes.php file in the application/config/routes.php location
and add this
$route['how-to-use-multidimensional-arrays'] = 'home/how-to-use-multidimensional-arrays';
This will work as URL as SEO friendly
Add this to ur root .htaccess file :
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
Then in ur index.php get the title $title = $_GET['title'] and do what ever you like with it like do a query in ur database and return data based on the title !

Mod rewrite and dynamic URLs

i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.
Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]
Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]

how to redirect a URL according to get variables in URL

I am trying to change my website URL according to get variables so that I can increase the security of my website.
For example I want to change my address, this is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1
And my website URL is:
http://joinexam.in/category.php?cat_id=17&mode=full&start=36
I want to convert this URL to:
http://joinexam.in/category/1736
where cat_id = 17
and start= 36
So it will become 1736 after the category.php page, I am trying to do it by using .htaccess file.
Here I want to take both cat_id and start as get variable, then according to these get variables, I want to change the URL of my website.
Can anyone explain the correct way to achieve this?
.htaccess
This forwards every URL to index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
Get the "original" URL inside index.php:
// this gives you the full url
$request_uri = $_SERVER['REQUEST_URI'];
// split the path by '/'
$params = split("/", $request_uri);
Then you might route based on these $params.
As a good and fast router lib i suggest: https://github.com/nikic/FastRoute
By using this, you don't have to mess around with htaccess regexp stuff and can keep things at the PHP level :)

url rewrite using php and .htaccess

this question is Basic but important. i need to rewrite php url using .htaccess . e.x one : i have many file with .php extension . e.x two : i need seo url friendly with name and html extension.
example 1 :
http://mydomain.com/editname.php
http://mydomain.com/location.php
.....
I need to rewrite all to :
http://mydomain.com/editname/
http://mydomain.com/location/
example 2 :
http://mydomain.com/name.php?id=1
http://mydomain.com/state.php?id=1
rewrite to : ( john & bsw is really name and inserted to mysql)
http://mydomain.com/name/1/john.html
http://mydomain.com/state/1/bsw.html
NOTE : If non of this, redirect to 404 not found page.
thanks.
To redirect simple folders to PHP files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])/$ /$1.php [L]
And then do redirect your SEO friendly URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])/([0-9]+)/([^/])\.html$ /$1.php?id=$2 [L,QSA]
The RewriteCond lines check to make sure the request isn't already a valid file.

Categories