htaccess does not open file when requested - apache

tell me what the problem is, let's say there is a file web/quberty/2.jpeg, I go to the link host/quberty/2.jpeg, the site produces 404, although the rule is
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1
whole htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

Based on your shown samples, could you please try following. Since you are adding web directory/folder to all uris, then have it as your very first rule and keep it at very top of your htaccess rules file.
Also please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/web [NC]
RewriteRule ^(.*)/?$ web/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ web/index.php [L]
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.

The problem was that there was no flag, here's a working htaccess.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule (.*) /web/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

Related

Trying to remove .php exstension in Browser

I have an older php website where i try to remove the .php Extension from php files getting accessed and showing in the browser but i have a problem.
I tried to add the last 2 lines to my .htaccess, saved it and checked the website again but the .php exstension is still there when i try to access parts of the website, anyone know why ?
This is my .htaccess file.
RewriteCond %{REQUEST_URI} ^(.+)\~s$
RewriteRule ^(.*) https://website.com/stats.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~q$
RewriteRule ^(.*) https://website.com/generate_qr.php?url=$1 [L]
RewriteCond %{REQUEST_URI} ^(.+)\~p$
RewriteRule ^(.*) https://website.com/preview_url.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) https://website.com/url_redirector.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]

Rewrite URLs in .htaccess for replacing Query parameters with forward slash (id?=value)

I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn't the issue.
what I'm trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:
folder/medias/value
Instead of
folder/medias?id=value
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ 404.php [L,R]
With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for external rewrite.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for internal rewrite.
RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
You may try this code inside the /folder/.htaccess (create this file if it doesn't exist):
RewriteEngine On
# External redirect from /folder/media?id=val to /folder/media/val
RewriteCond %{THE_REQUEST} /(\S+?)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
# Internal rewrite from /folder/media/val to /folder/media?id=val
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
Trailing ? in first rule is to remove query string from original URL.
%{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.

htaccess - remove .php extension and keep variables

original url - example.com/stars.php?id=9&s=lorem-ipsum
want to be - example.com/stars/9/lorem-ipsum
here is my try - getting error 500
RewriteCond %{THE_REQUEST} \s/stars\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)\.php$ /stars.php?id=$1&s=$2 [L]
.htaccess and stars.php are placed in the root - example.com
pls help
With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule to handle external redirection to user friendly URL here.
RewriteCond %{THE_REQUEST} \s/(stars)\.php\?id=(\d+)&s=([^\s]*) [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]
##Rule to handle non-existing files/directories should be served by php files in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1.php?id=$2&s=$3 [L]

Error come with seo friendly url using .htaccess

Firstly I have coded my website in localhost than everything goes fine all URL work but when I upload it on the cloud (GCP) the URL is not working(It shows that the URL is not found)
the following code is of .htaccess file in localhost
RewriteEngine On
RewriteBase /blog_site/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^single_post/(.*)$ single_post.php?post_title=$1
RewriteRule ^single_post/(.*)/ single_post.php?post_title=$1
RewriteRule ^category/(.*)$ category.php?category=$1
RewriteRule ^category/(.*)/ category.php?category=$1
RewriteRule ^author/(.*)$ author.php?author=$1
RewriteRule ^author/(.*)/ author.php?author=$1
RewriteRule ^author/(.*)/(.*)$ author.php?author=$1&user=$2
RewriteRule ^author/(.*)/(.*)/ author.php?author=$1&user=$2
RewriteRule ^search/(.*)$ search.php?search=$1
RewriteRule ^search/(.*)/ search.php?search=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Also use base tag
<base href="/blog_site/" target="_self">
On cloud server the code of .htaccess file is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^single_post/(.*)$ single_post.php?post_title=$1
RewriteRule ^single_post/(.*)/ single_post.php?post_title=$1
RewriteRule ^category/(.*)$ category.php?category=$1
RewriteRule ^category/(.*)/ category.php?category=$1
RewriteRule ^author/(.*)$ author.php?author=$1
RewriteRule ^author/(.*)/ author.php?author=$1
RewriteRule ^author/(.*)/(.*)$ author.php?author=$1&user=$2
RewriteRule ^author/(.*)/(.*)/ author.php?author=$1&user=$2
RewriteRule ^search/(.*)$ search.php?search=$1
RewriteRule ^search/(.*)/ search.php?search=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
and the base tag is
<base href="/" target="_self">
The VM instance link of GCP such that you can see how website behave
website link
If I manually type the url http://your_IP/contact.php working. But if I use http://your_IP/contact is not working
Based on your shown samples only, could you please try following Rules once. Please make sure you clear your browser cache before testing your URLs. There were multiple issues in your htaccess file, like conditions were not there, multiviews(which we came to know later in comments was ON) which made issues in your code.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^single_post/(.*)/?$ single_post.php?post_title=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^author/([^/]*)/(.*)/?$ author.php?author=$1&user=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(category|author|search)/(.*)/?$ $1.php?$1=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ $1.php [L]
EDIT by OP:
Please before you go forward please check .htaccess is enable it. If you don't know is enable or disable then type ---- in your .htaccess file, if it shows 500 internal error then its enable or it's disable. Then try to enable by searching on internet.

Opencart - Clean url for specific pages only

I am using Opencart and I have some static pages also.
I don't want to mess up opencart's SEO options so is there a way to create clean URLs only for specific pages?
What I want is :
http://example.com/my-custom-page.php
to
http://example.com/my-custom-page
I am using standart htaccess file comes with opencart package
I have these lines:
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
If you wanted to remove the .php extension in your url just use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
or this
RewriteEngine on
RewriteRule ^(.*)$ $1.php
If .html then you can do
RewriteRule ^([^\.]+)$ $1.html [NC,L]
put the following code at main directory (root) .htaccess file :
RewriteEngine on
RewriteCond %{REQUEST_URI} my-custom-page.php
# the line above to choose only my-custom-page.php and if you remove it
# the code will remove all php pages extensions
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R]
The code will remove my-custom-page.php extension and it would be my-custom-page regardless of its directory