.htaccess ReWriteRule - Requested URL <rewritten filename> was not found on this server - php

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]

Related

URL rewriting in .htaccess doesn't works as it should

I am currently coding my own website in PHP and MySQL on WampServer (local). I've already rewrited some of my URLs successfully. But I'm having problems with one of them.
I want to display on a page all my articles listed in a category using the GET method. For the moment, I have the following URL : http://localhost/actuco/cat.php?id=xpS3cc&slug=amerique-du-nord and I would like to use and display this URL as http://localhost/actuco/c-xpS3cc-amerique-du-nord/ (which does contains exactly the same GET parameters than the original URL).
I have tried to do this with the following line in my .htaccess file
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
When I write the second URL in my browser, it shows me a blank page with no code lines at all. My first URL works perfectly.
I'm really lost and I really don't know how to fix it.
Here is the whole .htaccess file used on my website (all other URL rewritings in this file do work).
Options +FollowSymlinks
RewriteEngine On
RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^bio$ o.php [L]
Thanks in advance for your help !
just pest this code in your .htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
hopefully, it will work.
Solved ! Just forgotten to add slash before $ in this line
Before :
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
After :
RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
Now it works, but I still have a problem with multiple hyphens in slug described here : Multiple hyphen handling in .htaccess (URL rewriting)

.htaccess filepaths differing how to solve

I've made www.site.com/asd to redirect to www.site.com/index.php?page=asd. However, I would like to add one more thing, where www.site.com/products/asd would redirect to www.site.com/index.php?page=products&item=asd
I can't seem to get it working, I have this as my base code:
RewriteEngine On
RewriteRule ^(admin|pictures)($|/) - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !=/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
I've tried adding:
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&item=$2 [L]
in various locations of the .htaccess file, which either results in:
500 Internal Server Error
or
0 CSS files load in .com/products/asd And no pictures load in .com/anyfile
I understand it's path related, but is this a good way to go about it? Can I solve it somehow in the .htaccess file, or would I need to add some check that if it's product page and a product is shown, include ../css?
The ^(.+)$ regular expression gives you a "500 Internal Server Error" because the redirection matches to the rule. So you gets the following error:
Request exceeded the limit of 10 internal redirects due to probable configuration error.
You can use:
RewriteRule ^products/(.+)$ index.php?page=products&item=$1 [L,QSA]
RewriteRule ^([^\.]+) index.php?page=$1 [L,QSA]
So, the following URLs will be redirected to:
/products/asd to index.php?page=products&item=asd
/asd to index.php?page=asc
Change the last two lines like this :
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&item=$2 [L]
You should specify that in first rule a URI must come like /whatever/ or /whatever so the second will match either /whatever/whetever or /whatever/whatever/ and both will work individually without any interference.

.htaccess: RewriteRule not Working using just the value

I've done a lot of research to solve this problem but I still can't figure out why it won't work. Initially I had Re-written my urls to add a .html but I want to change that and keep them simple without any suffix.
Here's an example of original link
http://www.howwe.biz/artist?a=eddy-kenzo
I used the rule below to change the link above and add a suffix .html
RewriteRule ^([^/]*)\.html$ /artist?a=$1 [L]
The result is : http://www.howwe.biz/eddy-kenzo.html
I want to rewrite the original link to something like this
http://www.howwe.biz/eddy-kenzo
I've tried using RewriteRule ^([^/]*)$ /artist?a=$1 [L] to achieve that but I get a horrible server error
Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#howwe.biz and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.`
The eddy-kenzo in the url is the value. What's odd is that if I add the name which in this case is a, it works. See below:
RewriteRule ^a/([^/]*)$ /artist?a=$1 [L] is valid for http://www.howwe.biz/a/bebe-cool but I need this like this http://www.howwe.biz/bebe-cool
What is it that am doing wrong. Your help will be much appreciated.
Sorry if this was tl;dr.
This should work :
RewriteEngine on
#1redirect "/artist?a=foo" to "/foo"
RewriteCond %{THE_REQUEST} /artist\?a=([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite anyother request to "/artist?a="
RewriteRule ^([^/]+)/?$ /artist?a=$1 [NC,L]
Your rule
RewriteRule ^([^/]*)$ /artist?a=$1 [L] failed because it is rewriting any request to target url unconditionally , the target url matches the pattern and it rewrites to itself. You rule will work with the following conditions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /artist?a=$1 [L]
Try this:
RewriteRule \/(.*)? /artist?a=$1
It takes everything from behind the last / to the end, and adds it to $1.

changing .htaccess to skip .php from URL results in error for some web pages

I setup .htaccess file as follows to make elegant url by hiding .php extension;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now all of my pages work other than following three urls/pages;
www.auroracs.lk/about
www.auroracs.lk/enquiry
www.auroracs.lk/locate
for above URLS I get following error. (You may check online by trying that URL right now)
..............................
Not Found. The requested URL /about/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
..............................
But if I rename about.php to about1.php then www.auroracs.lk/about1 url works. same happens to other two php files also. Can any one help why its not working only for these three pages. Is it because my .htaccess conflicting with some other file or any advice is appreciated.
Replace your rule by this more correct rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteRule to redirect to php file not working

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.

Categories