.htaccess regex redirect and replace - regex

Going to show my lack of regex knowledge here but seem to be having trouble with the following:
I need to redirect the following example structure of a url:
www.example.com/blog-title-here/tags/this+is+a+long+tag+name
www.example.com/tag/this-is-a-long-tag-name
I need to redirect to just /tag and replace any instance of + with a -

You can use this recursive redirect rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*[ +].*)$" $1-$2 [N,NC,DPI]
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*)$" /$1-$2 [L,R=301,NC,NE]
This will redirect http://localhost/blog-title-here/tag/this+is+a+long+tag+name to http://localhost/tag/this-is-a-long-tag-name

Related

How can I redirect users from a url to another using regex and htaccess?

I want to redirect users from www.example.com/ANYTHING to www.example.com.
Is .htaccess the better way to do it? How can I do it?
If you really just have to remove everything after www.mydomain.com, then you just have to delete everything from the first / to the end of the URL:
Search for this regex
%^([^/]*).*$
and substitute with \1, as I did here. Note that I have used the % sign as delimiter instead of /, so I don't need to escape the / in the regex. (I could have used any other available symbol other than /.)
You'll need to use a mod_rewrite RewriteRule (as opposed to a mod-alias RedirectMatch) in order to avoid conflicts with mod_dir and the DirectoryIndex*1.
For example, in .htaccess (Apache 2.2 and 2.4):
RewriteEngine On
RewriteRule . / [R,L]
The single dot matches something (ie. not the document root) and we redirect to the document root.
However, if the document root is an HTML webpage that links to resources like JavaScript, CSS and images then you need to make exceptions for these resources, otherwise these too will be redirected to the root!
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png|gif)$ [NC]
RewriteRule . / [R,L]
*1 A mod_alias RedirectMatch directive such as RedirectMatch /. / ends up matching the rewritten request (by mod_dir) to the DirectoryIndex (eg. index.php) resulting in a redirect loop.

regex with query string containing?

I am trying to redirect the following URL:
url/efx.aspx?xxxxxxx
to url/car-audio/efx-hardware/amp-install-kits
However it is redirecting whatever contains efx.aspx with the letters without the ? sign. I was wondering how I can fix this?
for example it is redirecting the following:
domain.com/efx.aspxlsdkjfhlasdf
but it is not redirecting
domain.com/efx.aspx?lsdkjfhlasdf
here is the .htaccess rule I wrote. how can I correct it?
RewriteCond %{REQUEST_URI} /efx.aspx[^/]+$
RewriteRule (.*) /car-audio/efx-hardware/amp-install-kits [R,L]
You can use this rule:
RewriteRule ^efx\.aspx$ /car-audio/efx-hardware/amp-install-kits? [R=301,NC,L]
Query string is not part of REQUEST_URI hence [^/]+ after efx.aspx fails your rule.
Also ? at the end of target URI removes any existing query string.

htaccess RewriteRule for found id in middle of url

I need get objectid from seo url e.g When I use this rule:
RewriteRule ^company/([A-Za-z0-9-]+)$ /lt?openobjectid=$1
evrything is ok when I use example.com/company/12 but if use example.com/company/12/some-text I get server error.
It is because your regex pattern is not matching example.com/company/12/some-text.
You can try this rule:
RewriteRule ^company/([a-z0-9-]+)(?:/.*)?$ lt?openobjectid=$1 [L,QSA,NC]

Seo friendly url for specific page

I need to make a specific page url to user friendly
I have a page www.example.com/index.php?route=a/b
Which I want to be rewritten as -> www.example.com/a
I used this rule in htacces but its not working
RewriteRule ^/a$ index.php?route=a/b
Please help
Change your rule to:
RewriteEngine On
RewriteRule ^a/?$ /index.php?route=a/b [L,NC,QSA]
And remember that inside .htaccess leading slash in URI is not matched so ^a/?$ instead of ^/a/?$.
Reference: Apache mod_rewrite Introduction

simple .htaccess redirect : how to redirect with parameters?

My goal is simply to redirect:
/jsn.php?parameters to http://www.site2.com/jsn.php?parameters
I tried with
Redirect permanent /jsn.php(.)* http://www.site2.com/jsn.php$1
Query string parameters are automatically passed, you simply want to do this:
Redirect permanent /jsn.php http://www.site2.com/jsn.php
The (.)* doesn't work with the Redirect directive, you were probably thinking of RedirectMatch, but either way, you don't need it. And also (.)* should be (.*), otherwise the $1 backreference would only get the first character.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(jsn\.php)$ http://www.site2.com/$1 [L,NC,R=301]
You can use an explicit URL rewrite in your .htaccess file:
RewriteRule ^/jsn\.php\?(.*) http://www.site2.com/jsn.php?$1 [R]
Note: You need to escape . and ? because they are also regular expression characters.
If you have a problem using mod_rewrite, post the contents of your file.