htaccess rewritecond for url with asterisks - .htaccess

Some... fine person... has set up an incoming link to our site that looks like:
http://www.site.com/*our*-*services*/
I'd like to redirect it so it points it to:
http://www.site.com/our-services/
Ours is a Wordpress site so there's some rewrite stuff in our root htaccess file already. A rule that simply removes asterisks from the URL would do, but I can't figure out how to do that, so I tried the following - loosely based on copying the existing Wordpress rules - which isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/\*our\*-\*services\*/)
RewriteRule . /our-services/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It looks like this rule is being ignored - the errant URL currently redirects you to our default Wordpress 404 page. Clearly, I am fail: what should I have put as the rewrite condition?

I'd remove your first RewriteCond and then just use this rule:
RewriteRule \*our\*-\*services\*\/ /our-services/ [L]

Well get them to change there incoming link.
Asterisks are reserved in urls, so in this case it shouldn't be used:
W3 Url Recommendation
Allowing asterisk in URL

Related

.htaccess rules for a particular type of URL

Could you please help to get this work -
http://domain.com/gallery/category/another/slug
to
http://domain.com/gallery/
If URL contains word gallery and no matter what's the rest of part it will always send request to http://domain.com/gallery/
http://domain.com/gallery/category/another/slug -> will hit http://domain.com/gallery/
http://domain.com/gallery/another/slug -> will hit http://domain.com/gallery/
http://domain.com/gallery/more/more/slug -> will hit http://domain.com/gallery/
Currently my existing .htaccess looks like this -
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site/index.php [L]
</IfModule>
# END WordPress
If you want every gallery URL to rewrite to /gallery/ you can just do
RewriteRule ^gallery/.* /gallery/
That will look for any URL at your hostname whose path starts with (^) the word gallery and is followed by anything (.*) and rewrite it as /gallery/
You can add a couple of flags -- [L] if you want to stop evaluating other rules, [NC] if it should not be case sensitive.
You might need to play around with the order if you're adding this to a bunch of existing rules.

Rewrite rule not working as expected?

I have a URL with a parameter which I wish to make into sef URL:
want:
http://map.tautktiv.com/street.php?address=abc
to become:
http://map.tautktiv.com/street/address/abc
or
http://map.tautktiv.com/address/abc
have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)
these are the rules I tried:
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1
2.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /address/(.*)\.php street.php?address=$1
3.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1
the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.
tried also this per below suggestions
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
same result, nothing happens.
tried to go directly to page from main domain but same result:
http://tautktiv.com/map/streets/street.php?address=abc
First rule will redirect your ugly URL to the pretty URL.
Second rule will internally redirect it back so the user will not see the ugly URL.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]
# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]
If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.
The .htaccess should go inside the folder where street.php is located.
HTTP is US ASCII so your language would fail, it will redirect it to something like this:
/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C
Your best bet here would be to change the links to use /street/address/word instead of the php file directly.
This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.
Try this one:
RewriteEngine on
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
In your examples you'd missed ^ and $ in the second row of RewriteRule.
And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.

Using .htaccess to change directory in url

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

.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 rewrite for subdomain only

I'm trying to rewrite some parameters to beautiful links, but for a subdomain / a folder only. Unfortunately I can't get it to work, maybe also because there are some other rewrites in line before...
Heres my code:
<IfModule mod_rewrite.c>
# NON-WWW TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# WORDPRESS-BLOG
Options +FollowSymlinks
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# REDIRECT FOR SUBDOMAIN
RewriteCond %{HTTP_HOST} ^subdomain.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ index.php?cshort=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?cshort=$1&cid=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?cshort=$1&cid=$2&step=$3 [L]
</IfModule>
Basically only the last part is the one I want to rewrite to change URLs from something like
http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1
The other rewriting rules for www.example.com shouldn't get affected. Unfortunately my current codes only does the first two rules for the blog and the www, but nothing happens on the subdomain. What's wrong in my code?
When you say that you want to rewrite from http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1 you mean that you want the user to enter the pretty URL and to have it serve the full URL in the background, not that you want to redirect from the ugly to the pretty URL, right?
In your RewriteRules, what are you trying to accomplish with "(?:/)?"? As written, that doesn't make any sense to me. If you're just trying to match whether or not the directory path ends with a slash, you can do that as follows:
RewriteRule ^([^/.]+)/?$ index.php?cshort=$1 [L]
EDIT: Additional suggestions:
Move the "Redirect for subdomain" section above the "Wordpress Blog" section. Since the Wordpress rule applies to "everything that's not a real file or directory, regardless of domain" that should go last.
RewriteConds only apply to a single RewriteRule that follows them. For each of the three rules you have listed under "Redirect for subdomain", after updating them per the above suggestion, you need to repeat the two RewriteCond lines in front of the RewriteRule.

Resources