I have my htacess rewrite working, the pages are going to where they are supposed to, but the url bar changes and I dont want it to. I thought this was an INTERNAL redirect and whatever is in the URL would be displayed. It's not working that way.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))?$
RewriteRule . /%2 [L]
Basically, the url IS rewriting to the new URL. How do I get it to not do that?
Per your last comment, try this instead
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/?|/(.+))$
RewriteRule . /%2 [L]
It should match an optional trailing slash e.g. http://mydomain.com/somedir or http://mydomain.com/somedir/ and one with a directory after e.g http://mydomain.com/somedir/dir2
If whatever the %2 back reference is matching doesn't end with an extension, mod_dir might think that it's a directory. If it's missing a trailing slash, mod_dir will externally redirect the browser to the same URL but with a trailing slash. You could try turning DirectorySlash Off in your .htaccess file or server config.
edit
You can try to bypass mod_dir's by doing the directory check yourself and adding the trailing slash so mod_dir won't redirect you. It would look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))$
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^[^/]+(/(.+))$ /$2/ [L]
Related
i have an index.php in the following folder:
https://example.com/folder1/folder2/index.php
If the URL is
https://example.com/folder1/folder2/93j3h233j3
then redirect to the index.php but the URL should stay the same!
My idea: i call a php file without .php first and then try to redirect...
RewriteRule ^folder1/folder2/([^\.]+)$ /folder1/folder2/index.php?&%{QUERY_STRING}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder1/folder2/(.*)$ /folder1/folder2/index.php? [R=302]
This doesnt work.
And keep the name! but how?
Any Idea?
Thank you
You can have your rule as this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder1/folder2)/.+$ $1/index.php [L,NC]
There is no need to use your first rule.
I've modified my .htaccess file to force trailing slashes on all the pages but I'm wondering how to remove the trailing slash in a particular url page? Need help. For example:
abc.com/test successfully redirects to abc.com/test/
But I want to remove that force trailing slash in a particular url of the site,
abc.com/demo/ should redirect to abc.com/demo
Here what I have done so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/demo/
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Update:
To remove trailing slashes for multiple urls what would be the code, eg abc.com/demo2/ abc.com/demo3/ abc.com/demo4/ etc.. Any suggestions?
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?
The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
Im working on a server with the following htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
RewriteRule ^[^/]*\.html$ index.php
RewriteRule ^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/.*$ - [L]
Now they asked me to prepare a static page inside their server, lets call it http://www.myserver.com/mystaticpage.html
The problem is that when i try to access that url, it redirects to index.php. How could I alter the htacces file to address this problem without messing anything with the installed CMS?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond ${REQUEST_URI} !^/(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)
RewriteRule .* index.php
The rules you used before were redundant: if .* is rewritten to index.php then why also rewrite ^[^/]*\.html$ index.php to it? it already matched previous rule...
They also overlapped - since the three RewriteCond conditions were only applied on the first rule. So the second rule was also applied to static files on disk.
Also, the two rules that were listed last had no effect whatsoever. Either you needed to list them first, or not at all. I converted them to an additional RewriteCond since they were only attempted to avoid rewrite on certain uris
i want to change a url like : localhost/site/home.php?p=index to localhost/site/index
i use this code in my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
but when i write like localhost/site/home.php?p=profile.user i get the 404 error, and go to this link
localhost/profile.user
so how can i fix itthanks
Let's look at your rewrites first:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
This is relative rewrite: the replacement text home.php... does not begin with a slash. Relative rewrites in a per-directory context (<Directory> or .htaccess) require a RewriteBase directive to be configured, otherwise they do the wrong thing.
Secondly, your rule is backwards, If you want to rewrite the home.php URL to the site/index one, you have to put the home.php match on the left side, and the site/index on the right:
RewriteRule ^home.php?p=(.*) /site/$1
Notice that I have an absolute rewrite. This means that mod_rewrite will create a URL out of the rewrite by sticking http://example.com on it. A new request is internally generated now for http://example.com/site/<whatever>. We can get away without using RewriteBase since we have no relative rewrites.
As for your last question, it is not clear why when you access localhost/site/home.php?p=profile.user you're being taken to localhost/profile.user. I'm suspecting that it's your home.php script doing that, perhaps. You're trying to use mod_rewrite to hijack that particular kind of PHP request and send it elsewhere, right?
What you meant is probably: you want to rewrite this way:
http://mysite.com/index => http://mysite.com/home.php?p=index
So this should work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?$ /home.php?p=$1 [QSA,L]
Now if you want the opposite:
http://mysite.com/home.php?p=index => http://mysite.com/index
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /home\.php$ / [QSA,L]