I have url like this:
www.mydomain.com/index.php?param1=first¶m2=second¶m3=thirth
and I want to rewrite it to :
www.mydomain.com/first/second/third
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ index.php?param1=$1¶m2=$2¶m3=$3 [QSA,L]
Problem is, that I am getting error 404 (I think that server is looking for directory /first/second/third instead of letting index.php in root dir to GET parameters from URL). Am I wrong? Or how to deal with this problem?
Your regex is incorrect actually. You're using 3 back-reference vales in target but only 2 values are being captured.
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?param1=$1¶m2=$2¶m3=$3 [QSA,L]
Related
I'm working on a little custom MVC project in PHP and am having some issues with the htaccess. Originally, my htaccess was routing all traffic to index.php in my root dir, and passing the remaining path as an argument. This was working perfectly with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [NC,L,QSA]
The problem I'm having now is that I need to move the index.php file into a /public directory. I scoured the internet for answers, and found a code snippet that kinda works in that it seems to get there as long as it is just hitting /, but as soon as the url becomes /register/ or anything else it just 404's.
# Get rid of /public/ in the URL, and route all requests through
# the Index.php file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /public/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
I know that last line makes no sense in when there is the rewrite to index.php with ?path that seems proper to me (at least it passes the argument like I want!) but without both these lines it doesn't seem to work, and I've been trial-and-erroring this for hours. Hopefully someone can help out! Cheers!
Keep only this content in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]
I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.
been looking for several hours now without success.
I'd like all urls containing /en/ to ignore this part but continue with all other rules in the htaccess (like php to no extension rule).
EXAMPLE
www.domain.com/en/gallery -> should point to gallery.php in my server root
www.domain.com/gallery -> should ALSO point to gallery.php in my server root
(will have to take care of duplicate content here somehow later)
www.domain.com/en/contact -> should point to contact.php in my root
Thank you
U P D A T E
Like mentioned in my question I'd like all other rules to still be running.
Only this rule does not work with below solution as it should work with
www.domain.com/en/category1 AND www.domain.com/category1 so with or without EN.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.ico)$
RewriteRule ^([^/]+)/? /categories.php?rw=1&url=$1 [L,QSA]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(?:en/)?([^/]+)/?$ $1.php [L]
UPDATE:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(png|jpe?g|gif|bmp|css|js|ico)$
RewriteRule ^(?:en/)?([^/]+)/?$ categories.php?rw=1&url=$1 [L,QSA]
I have a subdomain pics.mysite.com that i use to load images, so the src of an image would be something like http://pics.mysite.com/image.png
When image is loaded the url is rewrited to another folder inside (pics_files) where are the files like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pics_files/$1 [L,QSA]
Works fine, but now i'm having a problem.
If the image doesn't exists it creates an Internal Server Error - 500, but i would like to be able to load an "error" image, like errorImg.png, but so far i can't.
How can i rewrite the url to that errorImg.png if image do not exists?
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^pics\.mysite\.com$ [NC]
RewriteRule ^$ /errorImg.png [L]
# if valid image file then load it from /pics_files/ directory
RewriteCond %{DOCUMENT_ROOT}/pics_files/$1 -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pics_files/$1 [L]
# if not-existing image file then load /pics_files/errorImg.png
RewriteCond %{DOCUMENT_ROOT}/pics_files/$1 !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /errorImg.png [L]
This is because you are capturing the entire URI and routing it into the pics_files directory. If the image doesn't exist, the !-f condition passes and the URI is rewritten again, so you end up with:
/pics_files/pics_files/image.png
which doesn't exist, so the URI is rewritten again:
/pics_files/pics_files/pics_files/image.png
which doesn't exist, so the URI is rewritten again, etc, etc.
Instead of checking if the URI isn't a file or directory as a condition, check that the target image actualy exists:
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/pics_files%{REQUEST_URI} -f
RewriteRule ^ /pics_files%{REQUEST_URI} [L]
My mission is to achieve
http://localhost/projectname/media/video
from
http://localhost/projectname/index.php?m=media&cmd=video
and also
http://localhost/projectname/home
http://localhost/projectname/index.php?m=home
Below the files helps to achieve my mission
RewriteRule ^([^/]*)$ /projectname/index.php?m=$1
RewriteRule ^([^/]*)/([^/]*)$ /projectname/index.php?m=$1&cmd=$2
the thing is that any external files such as javascript and css would not be taken into play.
i know my alternative is to put this is htaccess
RewriteRule ^([^/]*)/cmd/([^/]*)$ /projectname/index.php?m=$1&cmd=$2
which enables http://localhost/projectname/media/cmd/video
I would like to make it without the CMD. Any suggestions?
Change your rules to this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ projectname/index.php?m=$1&cmd=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ projectname/index.php?m=$1 [L,QSA]
And make sure your js, css, images files are using absolute path not the relative ones. Which means their paths should either start with http:// or /.
why not just pass everything to index.php and then handle/parse the path, e.g.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Then use
$_SERVER['PATH_INFO']
$_SERVER['ORIG_PATH_INFO']
$_SERVER['QUERY_STRING']
http://php.net/manual/en/reserved.variables.server.php