I made a php mvc for a school project, and I've never used htaccess before but I want to make my URLs look pretty...
Right now the URL's are /?controller=posts&action=index but I would like to have them show as posts/index which is Controller/Action.. I also have a folder called "assets" with has my css files and images.
Is this possible to do without breaking my assets folder and site?
Thanks :)
EDIT:
I tried the following code but if I go to /posts/index it shows the homepage & if I go to /?controller=posts&action=index it doesn't change the name or anything
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?controller=$1&action=$2 [L]
You need 2 rules to accomplish the entire requirements and use conditions to ignore real files and directories.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /\?controller=([^\s&]+)&action=([^\s&]+)
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /?controller=$1&action=$2 [L,QSA]
This will also most likely cause css to not load so you also need this in the head section of your html.
<base href="http://www.yoursite.com" />
Related
We have added a paging system inside our layout. When we go to /page/clan, the page about our clan gets displayed. (as its located in pages/clan.php).
To get /page, we used a htaccess script, which rewrites index.php?page=pagename into the /page/pagename I mentioned.
This is our current htaccess code for converting these urls:
RewriteEngine On
RewriteRule ^page/([^/]*)$ index.php?page=$1 [L]
However, We'd like to remove the /page part, so it's possible to just use /clan instead of /page/clan to open the clan page.
How can this be done and with what code?
Thanks!
Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?page=$1 [L,NC]
Rewrite condions make sure you don't rewrite any existing files or directories on the server.
[NC] flag makes the pattern case insensitive, so in you case example.com/fOo would also work.
I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1
I am trying to build an online store using dynamic php pages. Currently, .htaccess works with the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^online_store/category/(.*)$ category?uri=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?action=$1 [QSA,L]
My index.php is just built with a bunch of includes, so a flat link to browse a category would be:
domain.com/online_store/category/apparel
Index.php is including online_store.htm, which processes the URI properly.
What I'm trying to accomplish next is to add a rule for product URIs, which would tell index.php to include product.htm to process the given URI:
domain.com/online_store/category/apparel/t-shirt1
I thought it would be as easy as adding:
RewriteRule ^online_store/category/(.*)/(.*)$ product?uri=$2 [QSA,L]
But it doesn't work. Even my uneducated mind tells me that the previous rewrite rule for categories preempts this one since L is included - but when I remove it neither work. Can someone educate me on the proper way to process this? Logically, I need this to happen:
domain.com/online_store/category/(URI1)/(URI2)
rewrite domain.com/category?uri=(URI1)
rewrite domain.com/product?uri=(URI2)
Final Edit - I decided to scrap the idea of nesting the product within the respective category, turns out I had issues with the way I was getting the URI using my php script. Here's the .htaccess that is working for me:
RewriteRule ^online_store/category/([^/]+)$ /category?uri=$1 [QSA,L]
RewriteRule ^online_store/product/([^/]+)$ /product?uri=$1 [QSA,L]
You should do:
RewriteRule ^online_store/category/[^/]+/(.*)$ category?uri=$1 [QSA,L]
it has to do with greedy repetition. Basically, the dot matches any character, including the slash(/)
I have some urls in my website like this..
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more
Now I am trying to convert above urls to user friendly url
http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials
This is code in my .htaccess file that I am tried so far. But it doesn't work.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Anyone can help me to remove .php etension from my urls??
Thank you.
Get rid of the conditions you are using and instead use the below 2 lines in your htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^whatever/?$ whatever.php [NC]
This will work even if user types a forward slash after the name or not
If you want to make them dynamic, here's a good tutorial on that
for a proper implementation so as not cause confusion during deployment if you dont have access to the htaccess file of your server or if it is override. You can consider a library like toro-php http://toroweb.org/ for easy routing.
here is what I need. I have the url pearlsquirrel.com/profilecomments.php?u=eggo.
eggo being my username and the part of the dynamic url that changes. I would like to rewrite the url to say pearlsquirrel.com/eggo/comments, using .htaccess.
Here is what I have so far:
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ profilecomments.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profilecomments.php?u=$1
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel\.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1/comments [L,R=301]
but I just can not get it to work. Any help would be greatly appreciated. Thanks!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/comments$ profilecomments.php?u=$1 [L]
NOTE
If you've used relative paths for your images, stylesheets etc. you need to change those into absolute paths or relative paths using the servers root folder as base in order to get your site to display properly.
For example, it will think that images/image.png is /eggo/comments/images/image.png
But, if you instead add a preceding slash /images/image.png your file paths will always start from the servers root folder and your site won't get messed up when you're rewriting your URL's.
Your first rule, overrides all the rest.
What you are describing (if I understood correctly), you need to handle /user/comments by profilecomments.php?u=user
RewriteRule ^([a-zA-Z0-9_-]+)/comments profilecomments.php?u=$1 [L]
this should do it.