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
Related
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.
I have links to my website like:
http://codecordia.com/codeframe/view/page/blog?post=name-of-the-article
or
http://codecordia.com/codeframe/view/page/product?post=name-of-the-product
I want to change to:
http://codecordia.com/blog/name-of-the-article
or
http://codecordia.com/product/name-of-the-product
I currently have made a .htaccess file like this, but I can't shorten the URL.
--------htaccess-------------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
--------htaccess-------------
For your blog URL, you can use the following rule to convert your clean url to a .php file with a query string:
# URL
# INPUT http://codecordia.com/blog/name-of-the-article
# OUTPUT http://codecordia.com/codeframe/view/page/blog?post=name-of-the-article
RewriteEngine On
RewriteRule ^blog\/(.*?) /codeframe/view/page/blog?post=$1 [L]
View on htaccess.madewithlove.be
You should be able to create the second rule easily with this example.
I have a webpage with url website.com/app/ there is an index.php in the dir and also an .htaccess file.
I want a make this url into a clean url
website.com/app/?app=app1&id=12345
to
website.com/app/app1/12345
I was able to achieve this with this rule
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?app=$1&id=$2 [L]
RewriteRule ^([^/]+)index.php?app=$1 [L]
but I also want to be able to access the webpage with this url
website.com/app/app1
so that It'll mean website.com/app/?app=app1 But currently I get "Object not found!" when trying this.
Thank you!
Try this RewriteRule, it will leave you with the URL: website.com/app/app1
RewriteEngine On
RewriteRule ^app/([^/]*)$ /app/?app=$1 [L]
I'm answering my own qn here :
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /apps/?app=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?app=$1&id=$2 [L]
You're welcome
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]
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