I want to convert an URL like this:
http://somesocial.com/SomeSocial/success/profile?search_user=exampleuser01
Into a personalized URL like this: http://somesocial.com/exampleuser01
I've tried creating a folder with a file inside success/preview/user_preview.php
which is a copy of profile.php to get the parameter from 'search_user' with $profile_id = $_GET['profile_id'] and header('Location:'.'success/profile?'.$profile_id); To redirect the searched user for his original link.
Modified my .htaccess to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^success/preview/user_preview?search_user=
RewriteRule ^/?([^/]+)$ success/preview/user_preview?search_user=$1 [L]
This works perfectly, but every time I try to access a file from the main dir (i.e Index.php) it redirects me to http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php
Why?
Try reordering your rule wit more restrictive conditions:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([^/]+)/?$ success/preview/user_preview?search_user=$1 [L,QSA]
Related
I have created all the file in my folder
Now when I am accessing the product_2.php I am getting the URL
http://localhost/Test/product_2
Now I have to display the URL like
http://localhost/Test/product/product_2
I added RewriteRule ^product/product_2 product_2.php in the .htaccess but it's not working.
.htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^product/product_2 product_2.php
Would you help me out in this issue?
Try adding the following line to your .htaccess file.
Redirect /Test/product/product_2 http://localhost/Test/product_2
You should put it before and with a little change
RewriteRule ^product/product_2 product_2 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Anyway, this is not the best practice I would recommend.. you should redirect all of your calls to index.php and handle them there, something like that:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
then in index.php
$requested_url = $_GET['url'];
and from here you can decide what to do, you can include the wanted file or even do something more clever.
I have this in my htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
Which is great for redirecting all files from example:
login.php to /login or apply.php to /apply
Now how do I get the ID from ALL files in my directory to redirect example:
RewriteRule ^home/([A-Za-z0-9-]+)/?$ home.php?id=$1 [NC,L]
RewriteRule ^home/([A-Za-z0-9-]+)/home/?$ home.php?id=$1 [NC,L]
That takes home.php?id=72 and redirects it to /home/72 - (THIS IS WHAT I WANT TO ACHIEVE BUT FOR ALL FILES)
But at current I am having to add the same 2 lines in my htaccess for every file that queries the id=$.
Any help would be appreciated massively.
I am assuming you are rewriting try below rule,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)$ $1.php?id=$2 [QSA,NC,L]
I have a problem with .htaccess configuration.
I know how to ignore file extensions (e.g. php).
That's my code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
But I want to ignore specific a subfolder in the URI.
For example, I have a file in example.com/sites/test.php but in the address bar, I just want to type example.com/test. Any ideas?
You can change the RewriteRule
RewriteRule ^(.*)$ sites/$1.php
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sites/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /sites/$1.php [L]
What I am trying to do it simple:
I want to remove .php extension from file for which I am using following code and it is working
#for hiding.php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
############################-------- END ---------########################
Also I want to show a $_GET parameter on screen for which the link should be link this
localhost/123 which is representation of localhost/somepage.php?para=123
and for which I am using following code which is also working fine
#for parameter Display
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ somepage.php?para=$1
############################-------- END ---------########################
Problem: These codes are working fine when not written together in same `.htaccess` file. But when written together and When I have to access login page using localhost/login. .htaccess treat login as get parameter value. Any solution to this problem?
Try this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ somepage.php?para=$1 [L,QSA]
i used the htacess to remove the php extension and it worked but i also have a page called profile wich i shorten but that make the pages names being taken as a a profile user name, is there a way to fix this?
the results i want is like this:
localhost/path_folder/index.php to localhost/path_folder/index
and
localhost/path_folder/profile?username=name to localhost/path_folder/name
and my htaccess code for the removing extension is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
and the code for the profile page is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path_folder/profile.php?username=$1
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
## First try To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /path_folder/$1.php [L]
## if PHP file not found then load profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /path_folder/profile.php?username=$1 [L,QSA]
If you want to remove file extension
RewriteEngine On
RewriteRule ^path_folder/([^/]*)$ path_folder/$1.php [L]
Shorten URL for userprofiles
RewriteEngine On
RewriteRule ^path_folder/profile/([^/]*)$ path_folder/profile?username=$1 [L]