I am trying to make pretty urls and my htaccess code works but whenever there is an additional / in the url or a part is missing it's throwing 404 errors while the parameters are optional
localhost/1/en/home/ -> works
localhost/1/en/home/random -> 404 error
localhost/1/en -> 404 error
RewriteEngine on
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?id=$1&language=$2&page=$3 [L]
What can I do to tell mod_rewrite that the parameters are optional
You can try using this rule:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)(/.*)?$ index.php?id=$1&language=$2&page=$3 [L]
Related
I've got url like www.web.com/home and www.web.com/about with .htaccess file:
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule "home" "index.php?c=home&m=main"
RewriteRule "about" "index.php?c=home&m=about"
If I type something like www.web.com/asd, .htaccess will throw 404 error and direct page-not-found.html file
But, If I type www.web.com/homesss or www.web.com/about/a/b/c, the .htaccess will not throw the 404 error
How do I fix that? I use pure PHP
Use meta characters in rewrite rule to 1:1 match:
^ --- Matches the beginning of the input
$ --- Matches the end of the input
[L] --- Flag last to stop processing rule after match
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule ^home$ index.php?c=home&m=main [L]
RewriteRule ^about$ index.php?c=home&m=about [L]
In addition to #JarekBaran's answer...
If I type something like www.web.com/asd, .htaccess will throw 404 error
Not with the code you've posted...
ErrorDocument 404 http://www.web.com/page-not-found.html
When a request does not map to a resource, the above triggers a 302 (temporary) redirect to the http://www.web.com/page-not-found.html. There is no 404 HTTP response. Details of the request that triggered response are lost. /page-not-found.html is exposed to the end user. This is generally bad for SEO and delivers a bad user experience.
The 2nd argument to the ErrorDocument directive should be a root-relative URL-path, starting with a slash so that the error document is called with an internal subrequest. Apache then responds with a 404 status. The error document is not exposed to the end user.
(Very rarely should an absolute URL be used here.)
For example, it should be like this instead:
ErrorDocument 404 /page-not-found.html
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument
I am having an issue with my current .htaccess mod_rewrite configuration.
Here is my current code:
RewriteRule ^([^/]*)/([^/]*)\/$ /game.php?id=$1&title=$2 [L]
RewriteRule ^([^/]*)\/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\/$ /category.php?cid=$1&page=$2&title=$3 [L]
The above code properly rewrites my URLs. However, whenever I type in into my browser a directory that doesn't exist, like, http://www.example.com/non-existent-directory/ my homepage shows up rather than the 404 page I have set up.
Another note, the trailing slash seems to make all the difference. If I remove the trailing slash from the URL, i.e., http://www.example.com/non-existent-directory the 404 page properly shows up. If I add it, my homepage appears and not the 404 page as it should.
Does anyone know what I need to do to get my URL to rewrite, but also show the 404 page when I type a non-existent directory?
Thank you in advance.
it's the ErrorDocument direction; while one can strip the trailing slash, as well:
# Rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
...
</IfModule>
# HTTP Errors
ErrorDocument 404 /index.php?controller=frontend&method=error&id=404
I would like to redirect in htaccess
https://www.example.com/albumgallery.php?id=8
to https://www.example.com/example-gallery
i've tried :
RewriteCond %{QUERY_STRING} ^id=8$
RewriteRule ^albumgallery.php$ https://www.example.com/example-gallery? [L,R=301]
-- it works , but on page it says
Not Found The requested URL /example-gallery was not found on this
server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
any ideas what is wrong with that ?
You are getting a 404 not found error because www.example.com/example-gallery does not exist on your server. You need to rewrite this url to your existing url. for example when you type
https://www.example.com/albumgallery.php?id=8 into your browser your rule redirects it to https://www.example.com/example-gallery since this new url doesn't exist so your server returns a 404 error status. You need to add an internal RewriteRule to your htaccess to handle your new url.
Put the following right bellow your existing Rule
RewriteRule ^example-gallery/?$ /albumgallery.php?id=8&loop=no [L]
I added an additional perameter loop=no to the Rule`s destination it's to prevent infinite loop error so that the rule can not conflict with your existing RewriteRule.
I already tried some Rewrite Rules but none of them worked out:
RewriteRule ^example/%(.*)$ - [R=404,L,NC,B]
RewriteRule ^example/\%(.*)$ - [R=404,L,NC,B]
RewriteRule ^example/%(.*)$ - [R=404,L,NC]
What is the correct syntax for that purpose?
Edit:
It's not redirecting to my 404 page, instead it gives me an Error 400. How could that be?
RewriteRule ^(new-york/new-york-city/chelsea/10001) index.php [L]
The above code does not work. It gives me a 404 error in Codeigniter. However, the following code does work.
RewriteRule ^(new-york/new-york-city/chelsea/10001) http://anywebsitehere.com/ [L]
The RewriteRule just rewrites to index.php, which calls the default route, if you have one setup. If you want to go to your search, you must rewrite to the search controller and method and pass the pretty URL or part of it as arguments
RewriteRule ^new-york/new-york-city/chelsea/10001 index.php/search_controller/search_method/$0 [L]
This will call in application/controllers/search_controller.php the method search_method with the pretty URL as arguments
search_method('new-york', 'new-york-city', 'chelsea', '10001');