Rewrite in htaccess trying to do a transparent redirect: rules with [R=301] works but not [L] - apache

I have some rewriteRule in my .htaccess.
I'm trying to do a transparent redirect for all urls starting with _ (like: http://mydomain/_hello to http://mydomain/object/hello). Here's what I tried:
RewriteRule ^_([A-Za-z0-9_-]+)$ object/$1 [L] but it gives me a "Page not found" error.
When I change to [R=301,L] (not [R=301]), it works but the url changes in the browser.
My full .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule ^_([A-Za-z0-9_-]+)$ index.php/object/view_by_alias/$1 [L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I really don't understand what's happening?

Try this:
Add Options -Multiviews above the rules
Change back to [L]
Also, note that you don't need QSA since you're not adding anything to the query string.

Related

Mod Re-Write Struggling With Rule Not Working

So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100

Apache mod_rewrite alias a path to another path

I have a weird rewriting problem due to some Drupal issue. I know not how to fix the Drupal issue, so this will be a temporary fix:
The path /the%20campanile/([0-9]+)/([0-9]+)/ (preferably with slash optional) should be an alias for /campanile/([0-9]+)/([0-9]+)/.
I've tried:
RewriteBase /
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
except that fails with 404 (presumably rewriting wrong). I've also tried with wildcards (*), but to no avail. I looked through about 16 posts on StackOverflow and a few other forums.
The entire .htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
What am I doing wrong?
Not sure why you're regular expression is ^campy/([0-9]+)/([0-9]+)/$ when you're trying to match something like /the%20campanile/([0-9]+)/([0-9]+)/. Groupings aside, the campy will never match the%20campanile.
You need to keep in mind that the URI is decoded before being processed through mod_rewrite, so you don't want to match against %20, but just the space:
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
You also want these rules before your index.php routing rule, since the routing rule will rewrite it before it gets to your other rules:
RewriteEngine on
RewriteBase /
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]

Can't work out rewrite rule

I am trying to setup a rewrite rules(s) that will:
redirect anything but /path and files to another site
then if path is /path/*, rewrite the url to index.php
Here is my rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/path.*
RewriteRule !\.(js|gif|css|jpg|png|jpeg) http://www.domain.com [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA]
</IfModule>
For some reason it's not working.
Other than making sure that you have mod_rewrite turned on, you need to change your first condition to also check for index.php, because after the second rule, the process loops and you end up getting redirected to domain.com:
Change:
RewriteCond %{REQUEST_URI} !^/path.*
to:
RewriteCond %{REQUEST_URI} !^/(path|index.php)

Remove .html from URLs with a redirect

We have a website, unfortunately all the URLs have the .html suffix, its a Magento installation, Magento allows you to change this on the CMS, but again, unfortunately all this URLs with .html suffix have a good ranking in Google. We need to redirect to non .html.
So, consider the following scenario, we are rebuilding this site from scratch, so we have the same urls on the new site but without the .html suffix.
Now is: www.example.de/cool-shoes.html
Will be: www.example.de/cool-shoes
So www.example.de/cool-shoes.html will not exist anymore, and I've been trying a redirect with the .htaccess with no luck.
I've tried so far:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule (.*)index\.html$ /$1 [R=301,L]
and:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
but it doesn't seem to work...any ideas?
Ok so, after some research, and failing to achieve this with a rewrite rule, the following line of code worked:
redirectMatch 301 ^(.*)\.html $1
This is quite usefull to remove any url extension and avoid broken links, hopefully helps someone in the future...
cheers!
This will rewrite the url like so http://example.com/page.html -> http://example.com/page
# Remove .html from url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Here's the solution that worked for me.
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Follow the steps, and you'll be able to remove .html from url without modifying .htaccess file.
This should do the trick:
RewriteEngine On
RewriteRule ^(\w+)\.html$ /$1 [R=301,L]
Try this to putting in your .htaccess file
Redirect permanent www.mysite.de/cool-shoes.html
www.mysite.de/cool-shoes
this may be helpful to you
This is for URLs ending with .html /product/raspberrypi.html ---> /product/raspberrypi/ (/product/raspberrypi/index.php) the index.php is hidden. Took me awhile to figure this out. LOL...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You have to use 'REQUEST_URI' and add it before index redirect rules since it could be overridden by the application. Its important to know that its URI not a filename or directory we are trying to redirect, since the file names all have index.php in the root folders(Wordpress).

.htaccess redirect and add variables

our clients banner campain lauched today an the wrong url has been deposited.
So i try to redirect
website.com/wheels/?lang=en&sel=540
to
website.com/wheels/index.html?lang=en&sel=540
I tryed to put a .htaccess in the folder wheels which contains following:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/?$ index.html?lang=$1&sel=$2
But it doesn't work. Anyone has an idea, what i'm doing wrong?
EDIT
I found a existing .htaccess in the ROOT. It contains following code:
RewriteEngine On
RewriteBase /
RewriteRule ^(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php [L]
If I add the code folling code, nothing happens:
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
Btw, lang is not static, it can be en or de.
Try adding this to the .htaccess in the root directory of your website. The querystring parameters are carried through by default.
RewriteEngine on
RewriteBase /
RewriteRule ^wheels/$ /wheels/index.html [R=301,L,NC]
Use this in your ROOT .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
You could also do this by adding an extra RewriteCond in front of the existing RewriteRule
RewriteCond $0 !^wheels/