I am trying to rewrite url. Below is my code in .htaccess.
RewriteEngine on
RewriteRule ^home?$ index.php
RewriteRule ^contact?$ contact.php
RewriteRule ^about?$ about.php
RewriteRule ^logout?$ logout.php
RewriteRule ^products?$ products.php
RewriteRule ^single-products/([0-9]+)$ single-products.php?product=$1
The pages without parameters works fine. But when I click on the page 'single-products' with parameters it displays like
single-products/1
and images and css styles not worked. When I changed the path css/syle.css to ../css/style.css it works.
Another problem is I can't return to another pages. When I coming to single-products, and click another menu option, url will displays like,
single-products/about
and displays page not found error. I requires /about. My menu link should be
<a href="single-products/1>Product 1</a>
How can I solve these issues. I searched many sites for a solution and didn't find one. Anyone please help me.
Thanks in advance...
For folders to ignore such as CSS etc. try this before your last rewrite rule:
# Directories to ignore
RewriteRule ^(css|images|js)(/.*)?$ - [L]
You might also want to consider using this too, if you plan of having a robots file etc.:
# Ignore favicon & robots.txt
RewriteRule ^favicon\.ico - [L]
RewriteRule ^robots\.txt - [L]
RewriteRule ^sitemap\.xml - [L]
You are missing a closing quote:
Product 1
As for your other question it's hard to answer as you haven't said how you want it to work..
single-products/1
is how the rewite rule is set to work.
Related
I've a joomla website. Recentley I added a subdirectory to the root of the website which contains some pages which are not linked to the CMS in any way.
I wanted to remove the .php from the end of these pages as I was going to promote them over social media and wanted the URL's to be easier for users to remember.
The page in questions is:
http://www.mytestwebsite.com/share/thepage.php
So I added the following rule to my .htaccess file:
RewriteRule ^/share/thepage?$ /share/thepage.php [NC]
With the hopes that the URL would be http://www.mytestwebsite.com/share/thepage
and still load thepage.php but it's not working.
Use:
RewriteEngine on
RewriteRule ^share/thepage/?$ /share/thepage.php [NC,L]
No leading / in the first RewriteRule argument in htaccess.
And /? is for the optional trailing slash (not optional e with e?)
I researched for removing page extensions (Ex: /page.php to just /page) and I found this article: http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
So, I inputed this code into my .htaccess file. It really worked, but when I paste/type my index link at Facebook status with no trailing slash at the end, the Open Graph kinda bugs. There's no thumbnail, no right title and description.
Check it: http://www.aftercolors.com.br
Would the solution be remove the code from .htaccess and create subfolders with index files to have the pages without extension?
That's the code of my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can try this one for just single link:
RewriteRule ^page page.php [L]
If you want just to redirect that one simple file. It's better to use id's to get bigger spectrum.
Good day!
I have two scripts in my root folder:
/index.php
/home.php
Both of them receive get parameter and display different pages, f.ex:
index.php?page=info
home.php?page=about
1) When I try to do clean URI by this:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 #working ok
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1 #doesn't load css,js
I found three advices: rather apply path of css,js folders to htaccess, use tag or use absolute path.
I'm wondering are there any other ways to make it solely by htaccess. Or is it possible to change query string the way to remove "/" if there is one f.ex:
I insert
example.org/info/
And my string automatically becomes
example.org/info
2) My htaccess only works with index.php. How should I change my rule to work with home.php if there is "userhome" in query string to make such queries possible:
example.org/home/about
P.S
RewriteRule ^home/([a-zA-Z0-9]+)$ home.php?page=$1
Working the way I need, concerning my second question. However I haven't still done with the problem of css, js loading. Any tips please
The best way to handle CSS not loading is to use <base> tag. That way you can use relative URL's and it won't break anything with your rules.
This will go in the head section of your site..
<base href="http://www.example.com/" />
You can try these rules for your your requirements.
#Redirect to home.php
RewriteRule ^home/([a-zA-Z0-9]+)$ home.php?page=$1 [L]
#Remove the trailing slash
RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [R=301,L]
#Redirect to index.php
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 [L]
I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
I have a single page website that changes content based on variables passed through the URL with PHP.
For instance, my url displays as
www.mysite.com/index.php?section=home
www.mysite.com/index.php?section=about-us
so forth and so on, depending upon which link you click in the main navigation.
I want the urls to read as www.mysite.com/home or www.mysite.com/about-us.
My mod-rewrite feature is enabled because before I made this a one page site, it was functioning correctly.
I've tried this...
RewriteEngine on
RewriteBase /
RewriteRule ^home/?$ index.php?section=$1 [NC,L]
I've tried every suggestion I found on Google and on StackOverflow, nothing is working.
Any help would be appreciated!
I have an .htaccess for exactly the same purpose, and it's working beautifully. My code is below.
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?id=$1 [L]
Naturally, if index.php isn't your main PHP file, replace that with what is. Also, replace ?id with ?section if your example code is what your using.
What the [^/\.]+ means is saying I need you to find text that contains anything but a period (.) or a forward slash (/), and there has to be at least one character in it.
For an example, go to the website I'm using it on, Northside Aikido.
If you have any questions, please feel free to ask.
EDIT
Note, this code won't turn http://www.example.com/index.php?section=home into http://www.example.com/home automagically, it'll just mean that the latter link works like the former. If you want it to automatically replace it, you'll need more code than one line.
this is what im using:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?section=$1 [NC,L]
</IfModule>
Almost
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?section=$1 [NC,L]