404 error in url rewriting to get user-friendly urls - apache

I have a static html website on a shared hosting.
I would like to turn these html files to user-friendly urls.
IE: website.com/web-design.html becomes website.com/web-design/
I am using these rules so that the corresponding html file is served when I request website.com/web-design/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html
It works if I type in the browser website.com/web-design (without trailing slash), but instead with website.com/web-design/ I get a 404 error:
The requested URL /web-design.html/ was not found on this server.
I want both urls (with and without trailing slash) to work...
It seems that a slash is added at the end of the html file, giving a 404.
I can't figure out how to fix it...
Thanks for your help.

You should try your rule like this
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /$1.html [L]

Related

.htaccess to customize php url in folder

My php file is in this location:
https://example.com/store/store.php?id=1
and I want to rewrite it as:
https://example.com/store/store_name
I tried like below:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)/?$ store.php?id=$1 [NC,L] #Handle page requests
But it is not working for me.
With your shown samples, please try following htaccess Rules file. Make sure to place your htaccess Rules file in folder where store folder is present, place it along side with store folder NOT inside it.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/(.*)/?$ store/store.php?id=$1 [QSA,NC,L]

Rewrite rule to redirect two friendly urls to same file

I'm trying to redirect two different url to the same file.
The url are: http://localhost/CLIENTI/HOTEL/cp/amministratori and http://localhost/CLIENTI/HOTEL/cp/amministratori/test
Actually i currently see the first url, but when I open the second one i see a lot of error of files inclusion on client side (es js and css files on header/footer).
This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dashboard$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^amministratori$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^amministratori/test$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
that was all my htaccess code. The errors are 404 of bootstrap.js and main.js which contains custom code for the frontend. I've understood the problem but I've haven't undersood if there is a solution alternative to the leading slash, because my header and footer are included in php, so in /cp/amministratori they work as now, but obviously if I change with a leading slash they will not work anymore
With your shown attempts, please try following htaccess Rules. Make sure to place your htaccess file along with CLIENTI folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /CLIENTI/HOTEL/cp/amministratori/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dashboard/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [QSA,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

htaccess using QSA returning 404

I'm trying to direct a url for a blog post to a PHP page that takes a GET var using htaccess.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/post/(\w+)$ blog/post.php?title=$1 [QSA,L]
However, this just gives me a 404. I have confirmed that /blog/post.php?title=my-postis a valid url.
Since your title contains hyphens your rule should allow it:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/post/([\w-]+)$ blog/post.php?title=$1 [QSA,L,NC]

htaccess rewrite .html not required / is optional

I have a working website, with atleast 500 pages ranked in Google.
All pages have .html at end of page.
Now I want to remove .html of all pages, but let the pages in Google (with .html) keep there index.
After searching I cant find the correct answer.
I know the ? is for optional. I tried 2 Rules behind eachother but didnt work too.
Here is what my htaccess now is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
I tried with adding:
RewriteRule ^(.*)$ find_page.php?redirect=$1&%{QUERY_STRING}
So if URL contains no extension use this rule, else use the normal rule (with htaccess)
I should expect my rule should be something like this: ^(.*)(?\.html)$
So my goal is: With or without html should work, but .php shouldnt be work :-)
Why look for a complex solution?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.html$ find_page.php?redirect=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)?$ find_page.php?redirect=$1 [L,QSA]
This rewrites all request to that php script, adding the original "file name" as parameter "redirect" and preserves all query parameters. That is what you asked for in your question.
But a warning: you can do this and it will allow to rewrite requests to for example page "redirection" as .../redirection?somearg or .../redirection.html?somearg. But for google both request are completely different pages. This will not help you to preserve any ratings when shifting to the new request scheme.
And a general side note: if you have control over the http server configuration, then you should always prefer to place such rules in the hosts configuration instead of using .htaccess style files. Such files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used in two cases: if you do not have control over the http server configuration or if you require your scripts to do dynamic changes to your ruleset (which is always a very insecure thing).
Ok solved my problem.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
With this option there will be checked if the page has .html optional at end. If it has, will the first rule be matched, else will go further and use the second rule which has no html at the end
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html
You don't need find_page.php for redirection. As it mentioned in other answer http://server/folder/file and http://server/folder/file.html becomes the same for the user but different for the Google.
This does not affect to PHP, folders and other content. It just tries to add «.html» to requested URL if it does not point a file or folder.
I've checked, it works fine even user queries uri with anchor like 1.html#bookmark1

NoT found url to show contents of index.php

Hello i need to show the content of index.php for all the not found url,
For example
http://domain.com/random must show http://domain.com/index.php content
http://domain.com/random/random.html must show http://domain.com/index.php content
http://domain.com/random/rand/random.php must show http://domain.com/index.php content
I tried below code , but still i get not found error
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/(.*?)/?$ /$1/index.php [L]
The problem here is that you are including part of the requested path in the rewritten URL, by using the $1, which effectively inserts the first bracketed part of the rule into the new URL. This means that your request for http://domain.com/random/rand/random.php would try to return the file http://domain.com/random/index.php.
In addition, your rule woudn't match your first example, as that URL doesn't contain a /, which is required by your regex.
Instead, if the requested URL isn't a file or directory (or link), then just rewrite everything to index.php:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php [L,R]
Note that I've added the R flag, which means that the requesting system (the browser) will see that the URL has changed... not sure if this is what you want.