I'm trying to removing .php extension from the url using .htaccess i've found a view sites that told me how to do this and i'm using;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However when i go to mydomain.com/user/settings (that would be mydomain.com/user/settings.php without the .htaccess)
It works fine. However when i go to mydomain.com/user/settings/ (trending splash on the end) it gives my custom 404 error page i've looked around on here (& Similar Questions) ask and google but can't really seem to work out what part i'm missing out?
Thank You,
Flexer
Try this to handle the trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]
Related
I'm stuck in what appears to be an infinite redirecting loop, caused by rewrite conditions in my htaccess file.
The goal was first to get rid of the .php extension, which worked.
Then I wanted to rewrite site.com/profile?user=username to site.com/username, which also worked.
But now, only site.com/username works. All other urls are stuck in a loop 'too many redirects occurred trying to open...' except if I write the .php extension again. Then the page opens.
How can I cumulate both rewrite rules and have them both work together?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /profile.php?user=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Any help much appreciated, as I really do not understand this...
Thanks in advance!
I have a problem with rewriting urls to my files. What I am trying to do is making my little shop system a bit more SEO friendly. My problem is that it sometimes works and sometimes it doesn't. I have no idea what I should do or what I am doing wrong.
My UPDATED .htaccess file:
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkte/?(.*)$ products.php$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkt/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
For example the /shop/products link is not working but /shop/products/ is.
And for some reason if I want to open the link /shop/products?cat=besteck its redirecting me to: localhost/D:/xampp/htdocs/shop/products.php?cat=besteck but If I capitalize the b it's working fine..
I have no Idea what to do, please help me! (And dont just give a working code snippet explain why mine fails and yours works)
EDIT:
Just to clear things up I want /products, /products/ and /products?some_get_query to redirect to my products.php file. /product/some_seo_url should be redirected to product.php?url=some_seo_url. I tried adding a question mark after the forward slash in my RewriteRule and I also tried putting the ^products rule above the ^product rule. Nothing worked yet.
EDIT 2:
I updated my .htaccess code above and now nearly everything works. The only thing that still doesn't work is when I open /shop/products/?cat=fish or /shop/product/some_product, my resources aren't loading!
ErrorDocument 404 /shop/404.html
RewriteEngine On
RewriteBase /shop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/?(.*)$ products.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/?(.*)$ product.php?url=$1 [L]
RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]
First off your product rule also matches products so a rewrite like:
products/cat/fish becomes product.php?url=s/cat/fish
Which is not what you want, the easiest way to avoid that is to reverse the order so that the products rewrite comes before the product one but I've also added the Last flag ([L]) to be on the safe side; besides, once it's got the match you want it's better for it to stop looking.
To prevent recursive rewrite loops you need to specify that the rewrite only occurs when the redirect is not an existing file or directory (otherwise your product rewrite matches product.php and it loops - forever). That's what those RewriteCond lines signify.
Other than that it seems OK.
I've been testing this in WAMP and I can't get it to work. I believe WAMP is set up properly due to the error message I'm receiving and it works no problem when I don't use the .htaccess file.
I have a filename that I'm using for testing called Feedback.php. Instead of displaying as www.mysite.com/Feedback.php. I'm trying to get it to just be www.mysite.com/Feedback.
.htaccess file
RewriteEngine on
RewriteRule ^Feedback.php/?$ Feedback [NC]
The error message that I receive is
"Not FoundThe requested URL /Feedback was not found on this server."
Are there two files required for reWriteRule to work?
I'm also trying to get this to work for my index.php to just be www.mysite.com which may be a different monster.
What should my navbar links be? Right now they are Feedback Should href perhaps be href="Feedback" once I get this working?
EDIT: I had the ReWriteRule variables backwards. Instead of
RewriteRule ^Feedback.php/?$ Feedback [NC]
I should have
RewriteRule ^Feedback/$ /Feedback.php [NC,L]
The rewrite doesn't look to have taken place though as it still reads localhost/Feedback.php
You may use this to remove .php extension from your pages as well as omitting the index part to your website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /index
RewriteRule ^index$ http://yourwebsite.com/ [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
I currently moved my custom build cms to another environment and now my rewrite doesn't work anymore and i can't figure it out. Can someone give me any hints? The things that have been changed is going from ubuntu to CentOs and from PHP 5.5 to php 5.3. My htacces looks like this:
RewriteEngine On
RewriteBase /
#point css, images en js folders to Public folder
RewriteRule ^img/(.+)?$ Public/img/$1 [NC,L]
RewriteRule ^images/(.+)?$ Public/images/$1 [NC,L]
RewriteRule ^style/(.+)?$ Public/style/$1 [NC,L]
RewriteRule ^js/(.+)?$ Public/js/$1 [NC,L]
RewriteRule ^fonts/(.+)?$ Public/fonts/$1 [NC,L]
RewriteRule ^css/(.+)?$ Public/css/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
A friend of mine wrote this and is now on vacation. Thanks in advance
Update: mod rewrite seems to work, becouse it redirects me.
The problem seems to appear only on images, it works for my css and js, i don't think the problem is .htaccess related anymore
Is mod_rewrite even active on your new system or does the error occur with this single .htaccess file only?
If you didn't set up the rewrite module you can follow this guide:
http://www.ghacks.net/2009/12/05/enable-mod_rewrite-in-a-ubuntu-server/
I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.