I have a problem whereby google has indexed some pages with the wrong url.
The url they are indexing is:
http://www.example.com/index.php/section1/section2
I need it to redirect to:
http://www.example.com/section1/section2
.htaccess isn't my forte, so any help would be much appreciated.
The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications.
I suggest reading this short introduction https://httpd.apache.org/docs/2.4/rewrite/intro.html (15mins) and reference these 2 pages while reading.
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html
This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.
mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
Think %{REQUEST_FILENAME} as the the path after host.
E.g. https://www.example.com/index.html, %{REQUEST_FILENAME} is /index.html
So the last 3 lines means, if it's not a regular file !-f and not a directory !-d, then do the RewriteRule.
As for RewriteRule formats:
So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname. . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1, then replace with /index.php/$1. The final effect is to add a preceding index.php to the whole URL path.
E.g. for https://www.example.com/hello, it would produce, https://www.example.com/index.php/hello internally.
Another key problem is that this indeed solve the question. Internally, (I guess) it always need https://www.example.com/index.php/hello, but with rewriting, you could visit the site without index.php, apache adds that for you internally.
Btw, making an extra .htaccess file is not very recommended by the Apache doc.
Rewriting is typically configured in the main server configuration
setting (outside any <Directory> section) or inside <VirtualHost>
containers. This is the easiest way to do rewriting and is recommended
To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will cleanly redirect /index.php/myblog to simply /myblog.
Using a 301 redirect will preserve Google search engine rankings.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
Assuming the existent url is
http://example.com/index.php/foo/bar
and we want to convert it into
http://example.com/foo/bar
You can use the following rule :
RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]
In the spep #1 we first match against the request string and capture everything after the /index.php/ and the captured value is saved in %1 var. We then send the browser to a new url.
The #2 processes the request internally. When the browser arrives at /foo/bar , #2rule rewrites the new url to the orignal location.
Steps to remove index.php from url for your wordpress website.
Check you should have mod_rewrite enabled at your server.
To check whether it's enabled or not - Create 1 file phpinfo.php at your root folder with below command.
<?php
phpinfo?();
?>
Now run this file - www.yoursite.com/phpinfo.php and it will show mod_rewrite at Load modules section.
If not enabled then perform below commands at your terminal.
sudo a2enmod rewrite
sudo service apache2 restart
Make sure your .htaccess is existing in your WordPress root folder, if not create one .htaccess file
Paste this code at your .htaccess file :-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Further make permission of .htaccess to 666 so that it become writable and now you can do changes in your wordpress permalinks.
Now go to Settings -> permalinks -> and change to your needed url format.
Remove this code /index.php/%year%/%monthnum%/%day%/%postname%/
and insert this code on Custom Structure: /%postname%/
If still not succeeded then check your hosting, mine was digitalocean server, so I cleared it myself
Edited the file /etc/apache2/sites-enabled/000-default.conf
Added this line after DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
Restart your apache server
Note: /var/www/html will be your document root
Do the following steps
1. Make sure that the hosting / your pc mod_rewrite module is active. if not active then try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.
change this setting :
#LoadModule rewrite_module modules/mod_rewrite.so
to be and restart wamp
LoadModule rewrite_module modules/mod_rewrite.so
2. Then go to .htaccess file, and try to modify to be:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
if above does not work try with this:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
3. Move .htaccess file to root directory, where is index.php there.
www OR root folder
- index.php
- .htaccess
Some may get a 403 with the method listed above using mod_rewrite. Another solution to rewite index.php out is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
# Put your installation directory here:
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I have used many codes from the above mentioned sections for removing index.php form the base url. But it was not working from my end. So, you can use this code which I have used and its working properly.
If you really need to remove index.php from the base URL then just put this code in your htaccess.
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will work, use the following code in .htaccess file
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
I don't have to many bulky code to give out just a little snippet solved the issue for me.
i have https://example.com/entitlements/index.php rather i want anyone that types it to get error on request event if you type https://example.com/entitlements/index
you will still get error since there's this word "index" is contained there will always be an error thrown back though the content of index.php will still be displayed properly
cletus post on "https://stackoverflow.com/a/1055655/12192635" which
solved it
Edit your .htaccess file with the below
to redirect people visiting https://example.com/entitlements/index.php to 404 page
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
to redirect people visiting https://example.com/entitlements/index to 404 page
RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
Not withstanding we have already known that the above code works with already existing codes on stack see where i applied the code above just below the all codes at it end.
# The following will allow you to use URLs such as the following:
#
# example.com/anything
# example.com/anything/
#
# Which will actually serve files such as the following:
#
# example.com/anything.html
# example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.
Options +FollowSymLinks
RewriteEngine On
# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
try this, it work for me
<IfModule mod_rewrite.c>
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
For more detail
create .htaccess file on project root directory and put below code for remove index.php
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I'm trying do use mod_rewrite at my .htaccess but isn't working.
my url is http://gestor.samfbas.com.br/index.php?p=something
it should be http://gestor.samfbas.com.br/something
The file is in a subdirectory (gestor) in my host, where all the files are.
<IfModule mod_rewrite.c>
Options +FollowSymLinks +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
</IfModule>
Try this one (if index.php is in the root folder http://gestor.samfbas.com.br/index.php):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
This should work. Testet it here on my local maschine (No other server redirects or else, just a fresh xampp installation).
It redirect http://gestor.samfbas.com.br/something to http://gestor.samfbas.com.br/index.php?p=something without changing the url in the browser.
And additional to the question in the comment.
This URL part p= should not be known be outside users!
Better use a long var here like sadff34dngn4nil212ugn=, so nobody can call the index.php with parameters directly from outside. You can't prevent that 100% but the redirect parameter p= is only for internal use.
But its just my opinion on that.
Hopefully that helps a little.
Find the right way to rome ;)
I am using Codeigniter framework in PHP. My website is on apache server. Path: /var/www/example.com/public_html
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
But still I am getting this error on accessing this page: www.example.com/test/wallet:
The requested URL /test/wallet was not found on this server.
SCREEN SHOT
When I use www.example.com/index.php/test/wallet, then it's working...
this is my .htaccess that works on both wamp and live linux server running Centos + Apache and Ubuntu +apache
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /path-to-code-igniter-directory/index.php/$1 [L]
# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
I also made a video on how to do this here
https://www.youtube.com/watch?v=v-4VkR54vLU
(skip to 28:50 to see the .htaccess .I also put it in the description under the video)
hope that helps
problem is with your URL rewrite.
place this outside application folder
File name = .htacess
in side
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I think all of your .htaccess setting are correct. I used #Nassim's solution.
Originally, Problem was with the configurations in apache2.conf file. I just set AllowOverride All in /var/www and activated rewrite on server by this command:
sudo a2enmod rewrite
Then I restarted my server: sudo service apache2 restart
Thanks to every1 for support.
I'm trying to get my .htaccess file to forward all URLs to a single page with url parameters so that I can handle the page retrievals that way. What I want to happen is this: say the user types in http://mysite.com/users/dan it should forward to the page http://mysite.com/index.php?url=/users/dan.
Similarly, if the user accessed the URL http://mysite.com/random-link it should forward to http://mysite.com/index.php?url=random-link
Here is the code I tried in my .htaccess file, but it just keeps throwing a 500 error at me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond % (REQUEST_FILENAME) !-f
RewriteRule (.*)$ index.php?url=$1 <QSA,L>
</IfModule>
I UPDATED MY CODE TO THIS AND IT STILL THROWS A 500 ERROR
I changed the < and > to [ and ] and I removed the space after the % in the RewriteCond, but it still throws an error.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I'm a novice when it comes to .htaccess, so any help would be greatly appreciated, because I don't know what's causing the server to timeout.
Couple of problems
Rewrite flags go in square-brackets, eg
[QSA,L]
Your RewriteCondition syntax looks incorrect. Try
RewriteCond %{REQUEST_FILENAME} !-f
Just to be on the safe side, anchor your expression to the start of the string
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Lastly, RewriteEngine and subsequent modifiers requires the FileInfo override. Make sure your server config or virtual host <Directory> section for your document root has
AllowOverride FileInfo
Update
Here's a typical rewrite scheme from an MVC project. This will ignore real files, directories and symlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
I have a real weird issue
I'm playing around with .htaccess and trying to redirect all requests to the /test/ folder's index file.
My site lies in a folder /test/ in my local htdocs folder. No other files exist currenlty.
What I expect:
When I visit any url, (for example /test/category/one/) I should be redirected to /test/index.php
What happens
I get a 404 Not Found
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?__route=$1 [L,QSA]
I have tried setting RewriteBase /test/
This is as straight forward as it gets so why isn't it working?
I have a Wordpress site in another folder and that works flawlessly with custom rewrites.
I even copied the Wordpress' .htaccess contents to the test site's, substituting the rewrite base and last rule with /test/.
Wordpress' .htaccess: (which works on a seperate WP install on same server)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>
# END WordPress
I have been struggling with this for a while now and read quite a few SO articles with no help.
I even write a rewrite log file and now there shows nothing when I browse to the test site but a visit to the Wordpress site writes quite a few lines.
I am running XAMPP on a Win64 machine.
Any help would be greatly appreciated! =)
Update: Also, make sure the line endings in your .htaccess file are set appropriately. Apache can sometimes choke on anything that doesn't include the new-line (\n) character.
So it looks to me like you want to (at some level) emulate what WordPress is doing. Here's how I handled this case when I was developing some software that did the same thing:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.php [L]
</IfModule>
For files that exist (i.e. either the -f or -d test passes), we serve them up unchanged. Otherwise, we redirect incoming requests to index.php. Note that the /test portion of the path is not included in the RewriteRule, since the RewriteBase set up where we were starting from. So, in your example, I think it would end up being:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ /index.php?__route=$1 [L,QSA]
</IfModule>
FWIW, I'm no .htaccess expert. I've simply found this to work for me in the past.
Also, if you're on a shared host (like DreamHost), you may need to set up the appropriate rules for allowing default error documents. Some shared web hosts serve up a single file (failed_auth.html is one example) for error cases. If you're not filtering out that case, you may end up with a 404.
This should do the trick:
# Activate the rewrite module.
RewriteEngine On
# Ensure the requested URL is not a file.
RewriteCond %{REQUEST_FILENAME} !-f
# Ensure the requested URL is not a directory.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?__route=$1 [L,QSA]