Stripping "&Itemid=XX" out of my URLS using .htaccess - .htaccess

I need to strip out a certain part of my URLS being generated and want to use .htaccess.
Some links are being appended with "&Itemid=XX" after the .html.
Example:
http://www.site.com/conferences-and-events.html&Itemid=XX
XX could be one digit or four so I guess I need a wild card for that part. I know other questions have been answered related to stripping out certain parts of URLs using .htaccess but I can't seem to figure out how to get my specific string stripped out. Any help would be appreciated and sorry for being redundant and dense.

You'll want to use URL rewriting for that, something like this should work;
RewriteEngine On
RewriteRule (.*)&Itemid=\d{1,4}(.*) $1$2 [R]
Explanation: This regular expression matches anything ((.*)) followed by &Itemid= [1 to 4 decimals], followed by anything (another (.*)), and redirects ([R]) to the first anything concatenated with the second anything, thus taking out the &Itemid=xx part.

Related

How to redirect only when there is something after .html?

I have found that there are some people with bad syntax links to our articles.
For example, we have an article with URL
http://www.oursite.com/demo/article-179.html
The issue is that lot of people have linked back to this article with bad syntax such as
http://www.oursite.com/demo/article-179.html%5Cohttp:/www.oursite.com/demo/glossary.php
Now, I added the following ReWrite Rule in the .htaccess file to take care of such links.
RewriteRule article-179\.html(.*)$ "http\:\/\/www\.oursite\.com\/demo\/article-179\.html [301,L]
But this has resulted in a Redirect Loop message. How can we fix this issue via htaccess rewrite rule. Basically, we need something in our rewrite rule that works only when there is one or more characters after the .html. If not, then it should not redirect.
Any help would be highly appreciated!
With best regards!
Use + instead of *. * matches zero or more, which causes the pattern to match for the redirected path too, + instead matches one or more.
Also you should make the pattern as precise as possible, ie don't just check whether it ends with article-179.html, better check for the full path. And if this all happens on the same domain, then there's no need to use the absolute URL for the redirect.
There's also no need for escaping the substitution parameter like you did, it's treated as a simple string except for:
back-references ($N) to the RewriteRule pattern
back-references (%N) to the last matched RewriteCond pattern
server-variables as in rule condition test-strings (%{VARNAME})
mapping-function calls (${mapname:key|default})
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Long story short, theoretically this should do it:
RewriteRule ^demo/article-179\.html(.+)$ /demo/article-179.html [R=301,L]
or this if you really need the absolute URL:
RewriteRule ^demo/article-179\.html(.+)$ http://www.oursite.com/demo/article-179.html [R=301,L]

htaccess redirecting old urls

I'm working on my htaccess and I finally got a result I can deal with. First I've got this
RewriteEngine on
RewriteBase /
RewriteRule ^blog/([a-zA-Z0-9\-/\.]+)/?$ blog.php?id=$1article_title=$2 [L]
This helps show my urls like so:
https://www.mydomain.com/blog.php?id=10
To
https://www.mydomain.com/blog/10/title.com
https://www.mydomain.com/blog/10/
https://www.mydomain.com/blog/10
https://www.mydomain.com/blog/10/title.com/
Four things here...
First is that all of these are accessible. I only want the first to appear and the others to direct there.
Second is that the article title will have spaces. Is there a way to turn spaces into - rather than have spaces?
Third is...is it okay to have .com within the url at the end? It just happens to be part of a post article title.
Fourth is do I include the extra part of the url$article_title in the urls I use? I'm lost on that part. Is it the same as I did with the id?
Last I was curious to know if I needed the id to show up. Can I exclude that in some way?
If I have urls already in place that use blog.php?id=10 do I change that to the new one? If I can't find it I'd like the url to go to the first rewritten url. All of the possible examples should go to that first url. or one without the id.

Mod Rewrite Rule for Dynamic URL - Is this possible?

I've given myself a headache trying to figure out if this can be done. I have a forum that was recently migrated, leaving thousands of broken dynamic links.
A typical URL looks like this:
http://domain.com/Forum_Name/b10001/25/
('b10001' refers to the forum ID number and the last number refers to the page number.)
The new URL is formatted like this:
http://domain.com/forums/Forum_Name.10001/
(No page number. Also, notice the 'b' is no longer in front of the ID number.)
Is there a rewrite rule that can achieve this?
I'm not a rewriter, but following what I've read here, something like this should work:
RewriteRule ^([A-Za-z0-9-]+)/b([0-9])+(/[0-9]+)?/?.*$ forums/$1.$2/ [NC,L]
^([A-Za-z0-9-]+) says "begins with an alphanumeric string", then there's the /b constant, followed by [0-9]+ (one or more digits), and then an optional / with one or more digit (the page number, (/[0-9]+)?), and lastly, it ends with an optional slash (/?$).
If the URL matches that pattern, then it's rewritten to forums/$1\.$2/. \. escapes the dot (it's a wildcard), $1 is the first match of the pattern (that first alphanumeric string which is the forum name), and $2 is the second match, namely, the number after the b.
Finally, NC means pattern is case-insensitive, and L is "last" - so you don't process any other rule. I think that is most up to you, just read the linked article and pick the flags you need :)
Edit: corrected pattern checking with http://htaccess.madewithlove.be/
I think what you're looking for is
RewriteRule ^([a-zA-Z0-9_]+)/b([0-9]+)/.*$ forums/$1/$2/
Make sure the contents of the [] parts match the format you're using for forum names and ids.
For parameters, you probably want R=301 to force a permanent redirect.

Redirect files and folders matching a prefix pattern

I am trying to redirect all requests to subfolders and subfiles in a directory that start with 4 numbers (e.g. 2012) to another directory using the .htaccess file.
For example,
/results/2005-09-19-xxx/* to /event/2005-09-19-xxx/*
and
/results/2005-09-20-file.ext to /event/2005-09-20-file.ext
But not
/results/anything-else/*
From the documents I have looked at I believe I need to use a RewriteCond followed by a RewriteRule. I have been struggling to find a way to both match on the initial 4 numbers and still use them in the redirected link.
I have spent many hours trying to find a solution to this issue, any help or advice would be much appreciated. Thank you.
You don't realy need a rewrite condition, try this rewrite rule :
RewriteRule ^results/([0-9]{4}.*)$ /event/$1 [L,QSA]
This should do the trick:
RewriteRule ^result/([0-9]{4})-([0-9]{2})-([0-9]{2})(.+)$ event/$1-$2-$3$4 [L]
This pattern is: 4 numbers dash 2 numbers dash 2 numbers rest, written in a way that you can easily understand and manipulate at a later date if need be. Everything that fits the pattern will be rewritten - the rest will work as it normally does
Example:
result/2012-02-02-abcd.ext
fits the pattern and will be rewritten to
event/2012-02-02-abcd.ext

remove part of url via mod_rewrite

Is there any way to hide part of a Url via mod_rewrite. I am currently using part of the url, .htm, to split the page that is being requested and the query string.
Example
http://www.example.com/page/article/single.htm/articleid=8
This would let me know that the page requested is:
http://www.example.com/page/article/single
And the quest string is:
article=8
Ideally i would like the have this to work the same url without the .htm visible
http://www.example.com/page/article/single/articleid=8
The number of variables in the query sting varies as does the number of levels before the .htm so the rule would need to be dynamic
Thanks
To also do multiple querystring parameters, how do you want it to look? I started with this, which keeps this simple, then got trickier below.
http://www.example.com/page/article/single/articleid=8&anothervar=abc
Try this rule:
RewriteRule ^([^=]+)/(.+)$ $1.htm?$2 [NC,L]
This handles one or more querystring parameters, but does require at least one. This looks for anything without an = up to a slash, then everything else. Basically, it uses the = as the indicator of the path vs. the querystring portions; but actually splits it on the slash. (The NC is a habit of mine; not needed in this case, but when I leave it out I forget it when it's needed.)
To let querystrings be optional, so it could handle just
http://www.example.com/page/article/single
I found it easiest with two rules, instead of trying to mingle this into one rule:
RewriteRule ^([^=]+)$ $1.htm [NC,L]
RewriteRule ^([^=]+)/(.+)$ $1.htm?$2 [NC,L]
You can do something even prettier, using slashes for everything including multiple querystring parameters, like this:
http://www.example.com/page/article/single/articleid=8/anothervar=abc
It's a little hairy, but I think this works (couldn't let it go...)
Another rule handles replacing the slashes with ampersands, then doing the rewrite as above. This was easier to keep straight - maybe there's a way to do it all at once, but this was tricky enough for me:
RewriteRule ^([^=]+)$ $1.htm [NC,L]
RewriteRule ^([^=]+)/([^=]+=[^/]+)/([^=]+=.+)$ $1/$2&$3 [NC,LP]
RewriteRule ^/([^=]+)/(.+)$ /$1.htm?$2 [NC,L]
The first rule is as above, handling no querystrings at all. That just gets it out of the way.
The second rule is a loop LP, which is what I tend to find in examples whenever you have an unknown number of replacements. In this case, it's replacing the last querystring-slash with an ampersand, and looping until there's only one left (leaving that for the question mark in the third rule).
It's looking for the last one of these articleid=8/anothervar=abc where there are two parameters left. It replaces the slash with an ampersand like articleid=8&anothervar=abc
In words, it's looking for (and capturing in parentheses):
(not-equalsign) slash (not-equalsign equalsign not-slash) slash (not-equalsign equalsign anything)
This lines up as:
(not-equalsign) /page/article/single
slash /
(not-equalsign equalsign not-slash) articleid = 8
slash /
(not-equalsign equalsign anything) anothervar = abc
It replaces the last slash with an ampersand, and after looping, turns it into the first draft above: http://www.example.com/page/article/single/articleid=8&anothervar=abc . The third rule handles this as described above.
A note: These also assume all your urls will look like this, since they're going to tack on .htm to everything. If you want still allow explicit /something/page.htm then these rules would need to not-match on .htm if it's already there - something like that. Or maybe an initial rule up front that looks for .htm and just stops rewriting there. Or maybe only do this for the /page paths.

Resources