htaccess add 301 moved permanently to RewriteRule - .htaccess

I have this rule in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php
what it does is to redirect all url's index.php?somevar=# to index.php, however when it redirect's sends out code 302 and i want it to be 301.
I tried to add [R=301] after RewriteRule . /index.php but it doesn't work.
Some help is appreciated :-)

This thing does not do what you describe. Your rule sends every request to non-existent files to index.php. The URL:
index.php?anything
already calls index.php. You don't need any rewrite magic to handle that.
Also ...
RewriteRule . /index.php
is a bad practice. It does not cause you problems now, but it could in the future, when you modify the rules. You'd rather use:
RewriteRule .* /index.php

Related

Apache redirect directive not acting as final rule

I am trying to achieve a simple redirect - from /news to /insights
I have the following in my .htaccess file:
redirect 301 /news /insights
<IfModule mod_rewrite.c>
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]
</IfModule>
Whenever I've used the redirect directive before, any matched URLs would be redirected and no further rewrites in the file would be processed. That is to say, going to /news would send you to /insights, and the rewrite to index.php would not be processed.
However, with this current setup, going to /news sends me to /insights?p=news, so for some reason the rewrite to index.php is still being processed.
Furthermore, if I comment out the index.php rewrite, then I get sent to /insights as expected.
This isn't how I've usually experienced this working so am unsure why it's doing this.
I have also tried the following:
RewriteEngine On
RewriteRule "^/news" "/insights" [R=301,L]
This simply results in a 404 instead of redirecting, which I also do not understand.
I am aware I could do the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/news"
RewriteRule ^ /insights [R=301,L]
which does work, however, I don't really want to have multi-line rewrites for lots of URLs, and would like to understand why the other 2 examples do not work.
You just need to insert this rule before last catch-all rule.
RewriteRule ^/?news/?$ /insights [R=301,L,NC]
Place it just below RewriteEngine On line so that mod_rewrite engine executed this rule before other rule.
Make sure to test it in a new browser.

.htaccess RewriteRule seems not to work

I got a problem with my .htaccess here and altough I searched the web and tried many things, I could not find any solution...
I have a wordpress-installation with enabled permalinks. Additional to that, I need to rewrite another URL on this wordpress-installation, which does not belong to WP.
If a user browses to http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets, I'd like to show http://www.URL.com/jackets.html as URL. There should also be the possibility to directly browse to http://www.URL.com/jackets.html.
My .htaccess looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^jacket\.html$ http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets [L]
</IfModule>
# END WordPress
Does anyone have any idea why this does not work?
The WordPress rewrite rules are redirecting any requests to non-existant files through WordPress. The rewrite rule responsible for this, RewriteRule . /index.php [L], is marked with [L], indicating that no more rules are to be processed after it. In order for your rule to work, it must be placed before the WordPress rules, but after the line RewriteBase /.

htaccess wildcard redirect to query string

Quick question, and I've seen it asked hundreds of times, but I just can't seem to get it to work (shame on me).
I'm trying to redirect anything other than index.php to view.php?id=$1 where $1 is anything other than index.php - but I can't seem to get it to work.
For example:
http://domain.com/ should use index.php
http://domain.com/index.php as should this
but..
http://domain.com/sdgoi3 should use http://domain.com/view.php?id=sdgoi3 etc
I've tried a few things and gone down through the questions above but to no avail.
Anyone got a solution? Appreciated.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^$ /index.php [L]
RewriteRule ^index\.php - [L]
RewriteRule ^view\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /view.php?id=$1 [L]
The logic here is:
if the request URI is / rewrite to index.php
if the request URI starts with index.php, don't change and pass through
if the request URI starts with view.php, don't change and pass through
if the request is to a non-existing file or directory, pass to view.php with the id param
Maybe something like :
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{REQUEST_URI} !^view\.php
RewriteRule ^(.*)$ view.php?id=$1 [L]
?

.htaccess redirect everything uknown except a few defined ones

I want to rewrite /home to the index.php and /edit to edit.php, but everything else, like /asdfg I want to redirect to new.php
Both edit and home on their own work, but the (.*) takes priority over those rules, I tried it without the RewriteCond, but then nothing works.
RewriteEngine on
RewriteBase /
RewriteRule ^home$ /index.php
RewriteRule ^edit$ /edit.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /new.php [L,R=301]
I just need all undefined URI's to redirect to /new.php
I also need to keep the /asdfg in the URI, so I can grab it with JavaScript and show the appropriate content, an either show a page to let ppl make a new post or to show existing content.
I can't define all existing content, because that comes from a database, only a few predefined, reserved "/folders" will redirect to predefined pages.
I have the following working:
RewriteEngine on
RewriteRule ^home$ /index.php [R]
RewriteRule ^edit$ /edit.php [R]
It leaves the /home there and go to index.php
Now I just need to redirect all unknown requests to a default page new.php.
I basically want to prevent users getting a 404 or any other error, ever..
You need to add the last flag to your first RewriteRules, so that the last RewriteRule does not take effect:
RewriteRule ^home$ index.php [L]
RewriteRule ^edit$ edit.php [L]
Try adding the [L] flag to the "home" and "edit" rules, so that the "new" rule isn't processed once they're matched.

htaccess ErrorDocument 404 conflict with RewriteEngine On

I'm using the below in my htaccess file to strip out the .php extensions on my files. It works fine.
However, I also want to utilize ErrorDocument 404 /index.php to redirect any non existing urls back to the homepage. If I use ErrorDocument 404 /index.php alone in the htaccess, the redirects work fine. Also, if I use the below Rewrite code it successfully strips the php extensions. I would like both of these to work together, however.
For example, if a user enters "www.domain.com/register", they go to www.domain.com/register.php. But if they enter "www.domain.com/something", and something.php doesn't exist, then they redirect back to index.php. I've tried every permutation of both of these together in my htaccess to no avail. Any help would be appreciated.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Not sure what the problem you're having is exactly, but if I change your rules to this, it works for me:
ErrorDocument 404 /index.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
So instead of checking if the request isn't accessing an actual file/directory that exists, check that the .php version of that file DOES exist.

Resources