How to change "+" to "-" in URL using htaccess - .htaccess

I want to change "+" to "-" in URL in php application by using htaccess
Example
http://hddir.com/videos/kodak+black
To
http://hddir.com/videos/kodak-black
My htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteRule ^videos/(.+)/(.+)/(.+)/(.+) search.php?q=$1&order=$2&type=$3&ptk=$4
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+) [NC]
RewriteRule ^ videos/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^videos/(.+)$ search.php?q=$1 [L,QSA,NC]
RewriteRule ^video/(.+).html$ watch.php?v=$1
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
#######Options -Indexes
ErrorDocument 404 /index.php

The following should do the trick by redirecting every domain with a plus in it permanently to the same domain but a minus instead of plus:
RewriteEngine On
RewriteRule ^(.*)\+(.*)$ /$1-$2 [L,R=301]

Related

Remove Query String from 301 Redirect through htaccess

I am facing a couple of 301 redirect issues.
Issue 1:
I want to redirect some links to another URL of the same site:
Redirect 301 /path-example.html /dir/subdir/
The redirect is working but it's generating query string like this:
example.com/dir/subdir/?str=path-example
How do I remove the query sting ?str=path-example ?
Issue 2:
I want to redirect some links to the homepage:
Redirect 301 /some-path.html /
But it's leaving a ? after a trailing slash like this:
example.com/?
How do I remove /? from the forwarded URL?
Existing rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^$ / [R=301,L]
Thanks!
Please place these rules top of your htaccess files(in case you have more rules in it). Please make sure to clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^index\.php/?$ - [L,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]
RewriteCond %{QUERY_STRING} ^s [NC]
RewriteRule ^/?$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
EDIT: My first answer without OP's other rules set is as follows:
RewriteEngine ON
##Redirection with removing query string rule here.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\?\S+\s [NC]
RewriteRule ^ /dir/subdir? [R=301,L]
##Redirection to site's home page here for html urls.
RewriteCond %{THE_REQUEST} \s/[^.]*\.html\s [NC]
RewriteRule ^ /? [QSD,R=301,L]

Catch all redirect

This is the htaccess file. I'm wanting to add a catch-all 301 redirect RewriteRule.
This is not working. Any ideas?
RewriteRule ^/(.*)$1 http://www.domain.co.uk/ [R=301,L]
Here are the rest of the rewrites.
enter coOptions +SymlinksIfOwnerMatch +MultiViews
RewriteEngine On
RewriteBase /
### HTTP > HTTPS & non WWW to WWW version
# RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\sHTTP [NC]
RewriteRule ^ http://www.domain.co.uk/%1 [NE,L,R=301]
### index.php & index to root domain
RewriteRule ^index(?:\.php)? http://www.domain.co.uk/ [R=301,L]
### special rewrite rules for shortened urls ##
RewriteRule ^section(?:\.php)?/(.*)$ /s/$1 [R=301,L]
### To remove section page if 1 which is default for most (unless products span more)
RewriteRule ^s\.php/(.*)/1/(.*) /s/$1/$2 [R=301,L]
RewriteRule ^product(?:\.php)?/(.*)$ /p/$1 [R=301,L]
RewriteRule ^article(?:\.php)?/(.*)$ /a/$1 [R=301,L]
RewriteRule ^discount(?:\.php)/(.*)$ /d/$1 [R=301,L]
### Core Jshop .php removal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\.php/(.*)$ $1.php?$2 [L,QSA]de here
Redirect 301 /blog http://www.domain.co.uk/a/8/nursing-home-news/
RewriteRule ^/(.*)$1 http://www.domain.co.uk/ [R=301,L]
If your rewrite rules are in an htaccess file then you need to remove the leading slash from your rule's pattern as RewriteRule's regex matches against a relative old path starting without a / .
Try :
RewriteRule ^(.*)$ http://www.domain.co.uk/ [R=301,L]
Make sure to clear your browser cache before testing this.

Exclude Subfolders from Rewrite rule htaccess

I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
So, my URL becomes http://example.com/title-of-blog and it is working pretty fine and awesome but my client reported today that admin section is not working properly and i checked and found that URL http://example.com/admin is getting converted to http://example.com/admin?prmn=admin because of above rule.
I googled how to exclude admin folder from Rewrite then i got below
RewriteRule ^admin($|/) - [L]
When i use this then when i type http://example.com/admin then m getting error page that i defined in htaccess.
Please help. My Full Htaccess is as below
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^admin($|/) - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# restrict .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^\ ]+\.php($|\ )
RewriteRule \.php$ / [F,L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
ErrorDocument 500 /error-page
I suppose there is an other rule that does something with /admin, but because you are using that second rule it will never reach that rule. Use a condition to exclude anything starting with admin instead. Also please note that the rule would rewrite it to /blogdetail?prmn=admin, so somewhere something is rewritten more.
RewriteCond %{REQUEST_URI} !^/(admin|blogdetail)
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]

htaccess search and replace plus sign with dash

I have form with two parameters city,keyword which are passed to search.php ,after submitting it redirects to url like
website.com/search/lenovo+laptop+dealers/Delhi
But i need url to be parsed without those plus signs but with a dash in url
Example: website.com/search/lenovo-laptop-dealers/Delhi
Code in my htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+search\.php\?keyword=([^&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]
RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?keyword=$1&city=$2 [L,QSA]
You can have your rules like this:
RewriteEngine on
RewriteBase /
RewriteRule "^(search)/([^+]*)\++([^+]*\+.*)$" /$1/$2-$3 [L,NC]
RewriteRule "^(search)/([^+]*)\+([^+]*)$" /$1/$2-$3 [L,R=301,NE,NC]
RewriteCond %{THE_REQUEST} \s/+search\.php\?keyword=([^&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]
RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?keyword=$1&city=$2 [L,QSA]

Domain Management using .htaccess

Suppose all the requests on A.com and B.com end up on the same server, and I want to control the request using htaccess.
The default www content root is /public_html/, but I want A.com requests to be forwarded to /public_html/A/ and B.com requests should be forwarded to /public_html/B/
I came up with this solution:
#/public_html/.htaccess
# A.com
RewriteCond %{HTTP_HOST} ^A.com$
RewriteCond %{REQUEST_URI} !^/A/
RewriteRule (.*) A/$1 [L]
# B.com
RewriteCond %{HTTP_HOST} ^B.com$
RewriteCond %{REQUEST_URI} !^/B/
RewriteRule (.*) B/$1 [L]
and I am having two problems with it:
A.com/A/index.php and A.com/index.php are the same thing, which is not cool! I'd rather the user be 301-ly redirected to the latter whenever he uses the former.
A.com/etc redirects to A.com/A/etc/
Overall, I don't want my visitors to see the /A/ (or /B/) in the URL anyway. Any htaccess solution is welcome.
You can use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# for external redirection of A.com/A/foo to A.com/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/*(A|B)(?:/(.*)|)\s [NC]
RewriteRule ^ http://%1.com/%2 [R=301,L,NC]
# for internal redirection of A.com/foo to A.com/A/foo
RewriteCond %{HTTP_HOST} ^(A|B)\.com$ [NC]
RewriteRule (?!^(A|B)(?:/(.*)|)$)^.*$ %1%{REQUEST_URI} [L]
Add this to your .htaccess in the same order:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(A|B)(?:/(.*))?$ http://%{HTTP_HOST}/$2 [NC,L,R=301]
#/public_html/.htaccess
# A.com or B.com
RewriteCond %{HTTP_HOST} ^(A|B).com$ [NC]
RewriteRule (.*) %1/$1 [L]
If you do not want A to be redirected to A when accessed like this A.com/B/. or If you do not want B to be redirected to B when accessed like this B.com/A/. You can throw a 404 error.
RewriteCond %{HTTP_HOST} ^A.com$ [NC]
RewriteCond %{REQUEST_URI} ^/B(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
RewriteCond %{HTTP_HOST} ^B.com$ [NC]
RewriteCond %{REQUEST_URI} ^/A(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
Add the above lines right afterRewriteRule ^ - [L] before RewriteRule ^(A|B)(?:/(.*....

Resources