Internal Server Error when url have twice file name - .htaccess

I am getting 500 Internal Server Error when i wrote the twice file name from the url.
I am using the following RewriteRule for the user Profile url:
RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,QSA,NC]
RewriteRule ^profile/(followers|friends|saved|stories)/([^/]+)/?$ $1.php?username=$2 [L,QSA,NC]
The Rules working working fine with this link:
https://www.thewebsiteurl.com/profile/username
But if i wrote the file name twice like this
https://www.thewebsiteurl.com/profile/profile/username
then i am getting the 500 Internal Server Error.
Is there any answer here to fix this problem ?
Here is my Full of my HTACCESS codes:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
ErrorDocument 404 https://www.websiteurl.com/sources/not-found.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,QSA,NC]
RewriteRule ^profile/(followers|friends|saved|stories)/([^/]+)/?$ $1.php?username=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin '*'
</IfModule>

Rules appear to fine though I have seen in some web servers that %{REQUEST_FILENAME}.php doesn't work properly due to file name resolution issues.
Can you try changing that rule to this:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

Removing the .php file extension from the URL Request

I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.
I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.
Here's what I got so far,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [R]
</IfModule>
I would like my domains to look like
https://www.example.com/about
Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.
Have it this way:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to clear your browser cache before testing this change.
Put in .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]
Redirect 301 /index /
remove .php from the link :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Multiple htaccess rewrite rules

I'm using the below htaccess file.First one is working fine.But the second one is not working.
RewriteEngine On
RewriteBase /Epropertiesnew/
RewriteCond %{THE_REQUEST} /Properties(?:\.php)?\?project=([^&]+)&property=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /ViewNewProjects(?:\.php)?\?newproject=([^&]+)&url=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ Properties.php?project=$1&property=$2 [NC,L,QSA]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ ViewNewProjects.php?newproject=$1&url=$2 [NC,L,QSA]
If I commented the first one,second one is working fine.Please help me to fix this.

Why is my .htaccess with mod_rewrite giving me a 500 error?

My .htaccess looks like this:
<FilesMatch ".(eot|ttf|otf|woff|svg)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L,NE]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/page?user=%1 [L,QSA,R=302,NE]
***THIS IS WHERE I INSERT THE NEW RULE***
RewriteRule ^([^/]*)-([0-9]+)\.html$ product?name=$1&product_id=$2 [L,QSA]
So far so good. It all works. When someone goes to scoobydoo.domain.com for example they are redirected to domain.com/page?user=scoobydoo. Now I also want to mask the page so that when someone goes to www.domain.com/scoobydoo they are really shown domain.com/page?user=scoobydoo, so I add this:
RewriteRule ^([^/]*)$ /page?user=$1 [L]
When I add this to the 2nd last line however, it doesn't work.
What's wrong here?
Your rule is ok, but you need to exclude your real files and dirs from it :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /page?user=$1 [L,QSA]

.htaccess REDIRECT_LOOP prevents PHP_SELF from executing functions properly

The following redirect is preventing a form with php_self from running functions on the same page properly.
I was having trouble removing the trailing slash for some reason on this server so I went this route. Found it interesting! The page refreshes but runs no code. Changing the page to prevent redirect goes back to working. Any thoughts?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^bio$ /bio/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^bio/$ /bio [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^bio/index.php$ /bio [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
You need to keep DirectorySlash off if you don't want / at the end of a directory:
DirectorySlash off
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule ^(.+?)\.php$ /$1 [L,R=301]
RewriteRule ^(.+?)/index$ /$1 [L,R=301]
# remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=302,L]
# route to bio/index.php if request is /bio
RewriteRule ^bio$ bio/index.php [L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

.htaccess no-html redirection vs directory name

I plan on implementing this .htaccess code (thanks anubhava) into my website:
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://mydomain.com/$1 [r=301,nc]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
ErrorDocument 404 /404.html
Basically it will change URL from "www.mydomain.com/this-page.html" into "www.mydomain.com/this-page" - it does that very well.
However, I have encountered an issue - let's say that one of my pages is "treatments.html".
URL adress will look like this "mydomain.com/treatments" and that's fine. But I want also to make a directory called "treatments" and include the list of available treatments in that folder, it would look like this
"mydomain.com/treatments/treatment-one"
"mydomain.com/treatments/treatment-two"
"mydomain.com/treatments/treatment-three"
and so on...
THE ISSUE IS:
When I try to access the file "treatments.html" browser confuses it with the directory and server sends message [403]Forbidden.
Any ideas for solution?
Ok change your last rule to:
DirectorySlash On
ErrorDocument 404 /404.html
RewriteEngine on
RewriteBase /
rewritecond %{http_host} !^mydomain\.com$ [nc]
rewriterule ^(.*)$ http://mydomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

Resources