.htaccess redirect and add variables - apache

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/

Related

Long URL in php using htaccess

I have a very long URL: //domain/administration/registrar/term_detail.php?a_id=47
And I want to make it looks like: //domain/administration/registrar/detail/47
I have created an htaccess file in //domain/administration/registrar/. Below is my code in htaccess and it is not working. Any suggestion of what I did wrong is appreciated.
Both of my htaccess and term_detail.php files are inside of registrar folder.
RewriteEngine On
RewriteBase /administration/registrar/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+registrar/detail\.php\?a_id=([^\s&]+) [NC]
RewriteRule ^ administration/registrar/%1? [R=301,L]
RewriteRule ^administration/registrar/([a-zA-Z0-9]+)/?$ detail?a_id=$1 [L,QSA,NC]
You can try these rules inside your /administration/registrar/.htaccess:
RewriteEngine On
RewriteBase /administration/registrar/
## External redirect to /administration/registrar/term_detail/47
RewriteCond %{THE_REQUEST} /([^./]+)(?:\.php)?\?a_id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]
## Internal rewrite to term_detail.php?a_id=47
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?a_id=$2 [QSA,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Keep your htaccess file inside registrar folder.
RewriteEngine ON
##External redirect to //domain/administration/registrar/detail/47 URL.
RewriteBase /administration/registrar/
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^/]*)/(?:[^_]*)_([^.]*)\.php\?a_id=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to term_detail.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2/term_$3.php?a_id=$4 [QSA,L]

Stop URLRewrite check after slash

Below is my current Apache configuration in .htaccess file
RewriteRule ^(user)($|/) - [L]
RewriteRule ^user/([^/]+) /user_/index.php?u=$1 [L]
I want to rewrite www.mydomain.com/user/test to www.mydomain.com/user_/index.php?u=test
But, I don't want to rewrite anything if it is only www.mydomain.com/user or www.mydomain.com/user/ where user is a directory in my server.
How can I do that?
Try this:
AddDefaultCharset UTF-8
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /?user/(.*)$
RewriteRule /?user/(.*)$ user_/index.php?u=$1 [QSA,L]
here:
RewriteCond %{REQUEST_URI} /?user/(.*)$ for check condition
You need to use quantifier .+ to make sure you don't match /user or /user/ like this:
RewriteEngine on
RewriteBase /
RewriteRule ^user/(.+)$ user_/index.php?u=$1 [QSA,L,NC]

Apache Redirect or Rewrite

I currently have an issue with my new website.
The live site is within a subdirectory called "live".
So, my URLs look like this: www.example.com/live/specials/
I would like my URLs to look like this: www.example.com/specials/
This is what I have in my .htaccess in the root dir:
RewriteEngine On
RewriteRule ^$ live/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ live/$1
Thanks in advance for your help!
You can use this rule in root .htaccess:
RewriteEngine On
RewriteRule ^((?!live/).*)$ live/$1 [L]
Place this in /live/.htaccess:
RewriteEngine On
RewriteBase /live/
RewriteCond %{THE_REQUEST} /live/(\S*) [NC]
RewriteRule ^ /%1 [L,R=302,NE]

mod_rewrite not working for query string redirect

Currently I have one rewrite rule for pretty urls, which is working fine. I tried adding in a second, and it's giving me problems:
# redirect /?p=xyz to /xyz
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ /index.php/%1 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The second one is working fine, but the first one is giving me internal server error. Basically I want anything in ?p=* to go to /index.php/*
[Edit] Just to clarify, I need http://domain.com/?p=test to be equivalent to http://domain.com/test/
domain.com/test to domain.com/?p=test
Try this instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !p= [NC]
RewriteRule ^([^/]+)/? /?p=$1 [L,NC]
OPTION:
In case it is the opposite:
domain.com/?p=test to domain.com/test/index.php
Try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} p=([^/]+) [NC]
RewriteRule .* /%1/index.php? [L,NC]
Remove index.php in case it is not needed, like this:
RewriteRule .* /%1/? [L,NC]

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).