I am using apache 2.2 in ubuntu 12.04 in Amazon EC2.
I enabled mod_rewrite using
sudo a2enmod rewrite
and able to see with
apache2ctl -M
now I wrote the .htaccess code as following and is not working
# Redirect everything in this directory to "good.html"
Options +FollowSymlinks
RewriteEngine on
RewriteRule .* good.html
Here is my contents of /etc/apache2/sites-available/default
http://pastebin.com/urNfmqan
when I add
LoadModule rewrite_module modules/mod_rewrite.so to the httpd.conf I get this
waiting [Thu Apr 11 18:44:12 2013] [warn] module rewrite_module is already loaded, skipping and htaccess is not working even then ( I presume in apache2.2 we don't edit httpd.conf to enable rewrite_mod but as per anubhava comments I modified the question)
Try this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /good.html [QSA,L]
</IfModule>
You should change the Base and the second element in the rewrite rule to whatever your directory is if it's not root.
Looks like you will have a rewrite loop. Apache rewrite doesn't run the rules once, but once for every time a path is requested. So on first load it rewrites .* to good.html. Then it makes an internal request for good.html and re-processes the rules again, rewriting again and makes another internal request to infinity. Revent's answer tries to solve this by checking if the file exists. You get index.html in his rules because index.html exists. If you removed index.html the file wouldn't exist, the rewrite condition would match and the rule would rewrite to good.html. When re-processing the rules, the file exists, the condition fails and redirect stops after that.
To fix, you can add the [L] flag after your rewrite rule to tell apache to stop processing rules after this one runs which should stop the loop. If that doesn't help, you can add a rewriteCond to check that the request_filename is not good.html causing the rule to fail after redirect.
Related
I get error 500 Internal Server Error when using the below lines in .htaccess file in the main directory of the website.
RewriteEngine on
RewriteRule ^ar/?$ index.php?lan=ar [L]
RewriteRule ^en/?$ index.php?lan=en [L]
My intention is load website.com/index.php?lan=en when entering website.com/en. What could be the issue here?
Have you check if the mod_rewrite is enabled on your apache server ?
If you are on Windows, check the httpd.conf file inside the apache/conf directory on your web server.
On linux like Debian / Ubuntu, check if there is the file rewrite.so in /etc/apache2/mod-enabled/ directory. Otherwise, add the module with the command a2enmod rewrite && service apache2 restart
I am also not seeing an issue here might be some other rule or conflicting htaccess, but you can do that easily by,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?lan=$1 [QSA,L]
I want to basically be able to goto, for example: www.example.com/my/folder/
where the subdirectories /my/folder/ don't exist, and have it redirect to index.php?path=/my/folder, while still keeping the original URL.
I hope this makes sense. I basically have no idea where to start with this. I'm guessing it's something with .htaccess, or http.conf or something, but I have no clue.
I basically want some direction on where to figure out how to do this, or some suggestions.
I always liked borrowing from the Wordpress .htaccess file myself
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This little bit of code will rewrite anything that is not a file and send it to your index.php file. You can then parse $_SERVER['REQUEST_URI'] for anything you need from the URL itself.
It depends on what web server you're running. If you're running Apache, then yes, it's in the .htaccess file or the VirtualHost file. In addition, mod_rewrite will need to be enabled on your Apache server for this to work.
sudo a2enmod mod_rewrite
sudo service apache2 restart
Other web servers will have it in different locations and require different commands.
I have a site that I would like to use Apache's RewriteRule to rewrite URLs.
I want:
http://baileyseymour.com/index.php?p=home
to rewrite to
http://baileyseymour.com/p/home
I have AMPPS installed on my Mac and I added the following lines to httpd.conf and they work successfully:
RewriteEngine On
RewriteRule ^/p/(.*) /index.php?p=$1 [PT]
I'm trying to do the same but on my server.
And I have added the same apache code to /public_html/.htaccess but I get the error message below:
Not Found
The requested URL /p/home was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The exact same code works on my localhost server. Why not on my website?
Can you check your remote server apache supports "AllowOverride All" ?
also try this way maybe it will help.
RewriteEngine On
RewriteRule ^p/(.*) /index.php?p=$1 [PT]
but you may have to modify $_GET['p'] properly. which will be sent only "home" part.
You need to remove the leading slash from the rewrite rule's pattern. URI's have their leading slash removed when the rewrite engine applies rules in an htaccess file.
RewriteEngine On
RewriteRule ^p/(.*) /index.php?p=$1 [PT]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.baileyseymour\.com
RewriteRule ^(.*)$ http://www.baileyseymour.com/$1 [R=301,L]
</IfModule>
.htaccess is the right place to put them and that file should be in the same directory as your default home page.
I have the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?id=$1
What I'm trying to achieve is this:
When the URL www.example.com/directory/10 is visited, the page www.example.com/directory/?id=10 is displayed on the browser without altering the appearance of the URL.
The above code creates a 500 Internal server error though.
Does anyone know where I'm going wrong?
Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^directory/(.*)$
Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^directory/(.*)$ directory/index.php?id=$1 [L,QSA,NC]
Above code has an extra RewriteCond %{REQUEST_FILENAME} !-f that will make sure to disallow subsequent execution of RewriteRule after first time since /directory/index.php will be a valid file.
I have got the same issue and found that "rewrite" module is not yet enabled in my case. So I need to enable it and then restart apache server:
Enable "rewrite" module: sudo a2enmod rewrite
Then restart apache server: sudo service apache2 restart
Hope this will help anyone.
You should try adding a forward slash to the front:
RewriteRule ^/directory/(.*)$ directory/index.php?id=$1
I've been caught out with that before.
Alternatively use the RewriteLog and RewriteLogLevel to debug, and look at the Apache error and access logs for further info:
RewriteLogLevel 3
RewriteLog ${APACHE_LOG_DIR}/rewrite.log
That will leave a log file in your apache log directory. In my case that is /var/log/apache
If you are using CodeIgniter and is in error problems 500. Follow the solution.
So to delete the segment "index.php" of URLs in CodeIgniter, you need to do 2 things. The first is to edit the /system/application/config/config.php file, changing the value of index_page policy to empty:
$config['index_page'] = '';
The second step is to create a file .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
And that's it! From now on, the URLs of your site/system made with CodeIgniter will no longer have the thread (called "annoying" by some) "index.php".
Another day searching for a strange error on Apache.
Working on my Docker Apache 2.4.48 alpine container. But not in production.
Here is the difference (just a dot):
Not working on hosting provider
RewriteRule ^public/(.*)$ ./public/index.php?route=/$1 [L,QSA]
Working on hosting provider
RewriteRule ^public/(.*)$ /public/index.php?route=/$1 [L,QSA]
Just uncomment #LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
Because by default it was disabled/commented
i am currently trying to learn about clean urls. i was using windows once.
i switched to ubuntu when suddenly my .htaccess seems to be not working
here is my htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
so i have this url
http://localhost/index.php/my_name/
i was expecting it to say the 'my_name' string in the browser
hello my_name
but it would only work whenever i add a '?' in the uri
http://localhost/index.php?/my_name/
i am pretty sure mod_rewrite is enabled. i even checked my phpinfo()
It sounds like the rewrite module is disabled. Edit your apache configuration and load the dynamic module for mod_rewrite if available. It would look like this:
LoadModule rewrite_module modules/mod_rewrite.so
If you're using Ubuntu, run sudo a2enmod rewrite to enable the module following by restarting the Apache server:
sudo /etc/init.d/apache2 restart
Found a solution. dumped the $_SERVER variable and used the REQUEST_URI index since QUERY_STRING does not display anything. will +1 Lekensteyn's answer since it led me to the right path