I am trying to move my project files from the root folder to subfolder.
Now all files are in subfolder.
I have this following htaccess code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /project/
ErrorDocument 404 http://localhost/404.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/([\w-]+)/?$ /profile.php?username=$1 [L,QSA]
RewriteRule ^profile/(followers|following)/([\w-]+)/?$ $1.php?username=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I must be change the RewriteBase / like this RewriteBase /project/ because of i moved php files in subfolder (project) . So after moved the files and changed the RewriteBase then the following code is not working
RewriteRule ^profile/([\w-]+)/?$ /profile.php?username=$1 [L,QSA]
So i get this error :
Not Found
The requested URL /profile.php was not found on this server.
The url is like this: http://localhost/project/profile/john
What i am missing here anyone can help me in this regard please ?
Note: I have move the htaccess file in project folder.
If i try to open the url without the fake folder (profile) like this:
http://localhost/project/profile?username=john then the page is opening without error.
The url normalize this:
http://localhost/project/profile/john
because of this rule:RewriteRule ^profile/([\w-]+)/?$ /profile.php?username=$1 [L,QSA]
but it is giving Not Found The requested URL /profile.php was not found on this server.
The #marcello-mönkemeyer comment is right. You just need to remove slash like this:
RewriteRule ^profile/([\w-]+)/?$ profile.php?username=$1 [L,QSA]
^
remove the slash / from here ---|---
Related
I´ve made some RewriteRules in my .htaccess and they all work if I call my Website from the Domain/URL of my server.
Now I wanted to point an external Domain to my subfolder on my server where my Website is located, but all RewriteRules aren´t working anymore. When I call the php-files everything is alright, but when I call my "beautiful" links, I wanted to create, then only error 404 is shown.
This is currently my .htaccess file:
ErrorDocument 401 /401.php
ErrorDocument 404 /404.php
RewriteEngine On
#redirect www request to the base domain
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#ignore existing files and directories
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Check if query string exists
RewriteCond %{QUERY_STRING} ^$
# Check that the request is not for an existing file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^spielplan$ spielplan.php [NC,L]
RewriteRule ^rangliste$ rangliste.php [NC,L]
RewriteRule ^alletipps$ allbets.php [NC,L]
RewriteRule ^benutzerseite$ userpage.php [NC,L]
RewriteRule ^einstellungen$ usersettings.php [NC,L]
RewriteRule ^spiel/([0-9]+)/?$ spiel.php?id=$1 [NC,L]
RewriteRule ^user/([A-Za-z0-9-]+)/?$ user.php?name=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php [NC,L]
I found a solution.
Because I tried to map my domain to a subfolder, this subfolder needs to work like an DocumentRoot and not like a subfolder.
So I needed to add:
RewriteBase /
in my .htaccess
I have a .htaccess file that should redirect all requests to index.php?url=$1, Except if the request points to images or specific files. If these are located somewhere undetr the public/ folder they should be served. If the requested file is an image somewhere out of the public/ folder then it is redirected to a default image, and if the requested file is a HTML, JS, CSS, SWF file and is out of the public/ folder a simle 404-not found should be sent back.
Everything works good except that when I request images from /public or let's say public/images they are redirected to the default image as well. I've tried everything, even the %{ENV:REDIRECT_STATUS} but still the same. Any Ideas how could I make the loop stop If the file has been served in the line 11.?
Here is the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
SetEnv HTTP_MOD_REWRITE on
RewriteBase /wsproject/
Options All -Indexes
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^public/.*\.(html|css|js|swf|jpe?g|png|gif|bmp|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif|bmp|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ public/errors/img-not-found.png [L]
RewriteCond %{REQUEST_URI} \.(html|css|js|swf)$ [NC]
RewriteRule ^ - [R=404,L]
RewriteCond %{REQUEST_URI} !^public/.*$ [NC]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
I use the code from a former answer, you can see here: Apache Rewrite module, complex rules
Use THE_REQUEST variable instead of REQUEST_URI as your last rule is rewriting everything to /index.php and changing value of REQUEST_URI.
<IfModule mod_rewrite.c>
RewriteEngine on
SetEnv HTTP_MOD_REWRITE on
RewriteBase /wsproject/
Options All -Indexes
DirectoryIndex index.php
RewriteCond %{THE_REQUEST} /public/.+?\.(html|css|js|swf|jpe?g|png|gif|bmp|ico)\s [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \.(jpe?g|png|gif|bmp|ico)\s [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ public/errors/img-not-found.png [L]
RewriteCond %{THE_REQUEST} \.(html|css|js|swf)\s [NC]
RewriteRule ^ - [R=404,L]
RewriteCond %{THE_REQUEST} !/public/ [NC]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
I have the following directory structure:
public_html/sites/site_a/
public_html/sites/site_b/
I am trying to write a .htaccess for site_a that rewrites urls. I have the following sitting in the root directory of site_a:
Options +FollowSymLinks
RewriteEngine On
RewriteRule \?page=([a-z]*) $1 [L,QSA]
Basically I would like to have my urls:
http://hostname/sites/site_a/?page=products
Show as:
http://hostname/sites/site_a/products
However this doesn't seem to work. The page shows with the full url.
Try:
RewriteEngine On
RewriteBase /sites/site_a/
RewriteCond %{THE_REQUEST} \ /+sites/site_a/\?page=([^&\ ]+)
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ ?page=$1 [L,QSA]
in the htaccess file in your site_a directory.
I have a website, and the builder put in this code into my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]*)(\.php)?(\?*)$ index.php/$1$3 [L,QSA]
But, what i also need is:
- remove slash if it is no directory
- remove extension .php
- remove index.php if it is in de url
Ive tryed many suggestions, but i think it is in the code that is already in de .htaccess.
i've managed to redirect the site to www. if not given in the url
Does anyone have an idea? i'm out of ideas
Before the rules that you already have, try adding these:
# remove trailing slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# remove index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.*)index\.php/?([^\?\ ]*)
RewriteRule ^ /%1%2 [L,R=301]
# remove php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.*)\.php($|\ |\?)
RewriteRule ^ /%1 [L,R=301]
I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mykeyword$ news.php [L,QSA,NC]
However, when I open the news.php, the url is still the same, that is www.mydomain.com/news.php instead of www.mydomain.com/mykeyword
I make the following test:
RewriteEngine on
RewriteRule ^test\.html$ test.php [L]
I upload 2 files on my server, test.html and test.php and after I type www.mydomain.com/test.html, my php page was displayed, so that mean that I have no problem with my settings. What on earth I am doing wrong???
Any help will be deeply appreciated.
Regards,Zoran
Change your .htaccess to this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+news\.php [NC]
RewriteRule ^ mykeyword [R=301,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
The rewrite rule translates from the URL supplied by the user to the URL seen by the server. Try browsing to www.mydomain.com/mykeyword - you should see the page news.php.