How to add multiple name in the URL? - php

I have know more button on my website and when the use click on know more button then I have to redirect to product.php page. Below is the code which i am using and it's working.
Know More
Now I have to rewrite the URL from http://example.com/product.php to
http://example.com/products/products2/products3/products
I tried below code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^products/products2/products3/products$ product.php [L]
but it's not working. Would you help me out with rewrite rules?
I tried this also
RewriteRule ^products/products2/products3/products product.php

Have it this way:
RewriteEngine On
# match specific URI
RewriteRule ^products/products2/products3/products/?$ product.php [L,NC]
# php estension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Note changed order of your rules and checking for presence of .php file before adding .php in front of a URI.

Related

Remove .php from url WITH exceptions (htaccess)

I have the following .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1
This basically serves my user profiles so like twitter someone can just type: url.com/theirusername
However I want to make certain pages like /home not lead to a profile but a different .php page, again without the .php at the end.
Can anyone think of a way to do this?
Thanks!
You can have another rule to add .php before this rule:
RewriteEngine On
# To internally redirect file to file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handle profile URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ account.php?username=$1 [L,QSA]
just add more rules to match all the other pages, put it before the rule for usernames.
RewriteEngine On
RewriteRule ^home$ home.php
RewriteRule ^contact$ contact.php
RewriteRule ^([a-zA-Z0-9_-]+)$ account.php?username=$1

How to rename url's using rewrite_mod?

I am not a professional but with help from SO I was able to rename the profile page URL's for users on my project using Rewrite_mod using:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
This works good. But I can't figure out how to give a specific name to a particular page. For example I have a page profile_settings.php when users go to that page I want it to be example.com/settings in the address bar. Tried few ways from SO like removing .php thought it will remove php extension for all files but It did not work at all. Now my .htaccess looks like:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Was hoping example.com/profile_settings.php to be example.com/profile_settings but it did not work it's still full address with file extension. Moreover It will be good if I can deal with single file separately as besides removing extension I also want it to give desired name.
Rewrites for specific cases like /settings -> /profile_settings.php need to be handled separately and before other generic rules like this:
RewriteEngine On
# specific rewrite rules
RewriteCond %{THE_REQUEST} /profile_settings\.php[\s/?] [NC]
RewriteRule ^ /settings [R=301,L]
RewriteRule ^settings/?$ profile_settings.php [L,NC]
# add .php extension if corresponding .php file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handle profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]

Multible RewriteRules result in Internal Server Error

forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]

Remove file extensions from my Php files

How can I remove .php extension from php files
htacess file
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
any help is much appriciated, Thanks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1.php [NC,L]
If you go to example.com/test/ it will load example.com/test.php
For hat you should define an entry point.
Then you can rewrite your URLs to a parameter of a specified file in that case the index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]
Then you can filter that parameter in your script and show the correct content.
You have an URL something like:
www.example.de/test-test
Then everything is rewritten to your index.php. Other possibility if you rewrite to an existing file. You can do something like this:
RewriteRule ^(.*)/?$ $1.php [NC,L]
Then you fetch everything behind the slash and call an existing PHP file.
Remove .php extension with .htaccess

.htaccess redirect html to php and access to php files without .php extension

I wish to accomplish 2 things with this .htaccess file.
1) All requests to .html point to .php
eg. user goes to: http://www.mywebsite.com/contact.html
Browser loads: http://www.mywebsite.com/contact.php
2) Request to http://www.mywebsite.com/contact will load the contact.php
(This should apply to all pages not just the contact page.
Here is my .htaccess file.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
To be honest I have no idea what these are doing. I blended them together from a mismash of articles I read. Any help would be appreciated.
Try something like this. Follow the comment thread for security implications, but this is what you're asking to do
RewriteEngine On
RewriteBase /
# Redirect HTML to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ $1.php [L]
# Otherwise, try PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]
# Lastly, fallback to error page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.php [L]
I need to explain something to you about htaccess file roles;
In this example, htaccess will try and match patterns, as you need in your example;
If a pattern is matched, then some action will happen;
you mark with () all the data than you need to be extracted from the url
For each rule, the first () will have the $1 id, the seccond () will have $2 and so on
if $1 is 'help' it will load the 'helpme.php' file maybe, and not the 'help.php' file; it will load the file that you want it to be loaded;
using $1, $2 ... you can pass parameters and values to the real/translated url request
in my example, i wanted to always use the index.php file, you will use whatever file you need
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]
RewriteRule ^(member)-([0-9-]+)$ index.php?action=member&id=$2 [NC,L]
RewriteRule ^([a-zA-Z-]+)/([a-zA-Z-]+)-([0-9-]+)$ index.php?action=$1&saction=$2&sid=$3 [NC,L]
RewriteRule ^([a-zA-Z-]+)/([0-9-]+)$ index.php?action=$1&id=$2 [NC,L]
RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/(.*).html$ index.php?action=details&id=$1&p1=$2&p2=$3&p3=$4 [NC,L]
If you are wanting all of your .html files to point to .php file a simple way of doing so would be
RewriteRule ([\w\d\-_]+)(\.html)? $1.php [L]
This allows you also to use sub-directories
contact.html would redirect to contact.php
/subdir/page.html would redirect to /subdir/page.php and so on

Categories