Looking to rewrite a url with a hash using htaccess - .htaccess

I have a url with the format http://www.site.com/index.php/page and want to rewrite it to http://www.site.com/#/page but I can't seem to find an answer anywhere. Can anyone help?

If you simply want to replace index.php with # you can do something like this:
RewriteRule ^/index.php/(.*) /#/$1 [NE,R]
The NE flag should keep mod_rewrite from escaping the #

Related

htaccess to add ?query=string to specific url

I'm pulling my hair out trying to get a .htaccess rewrite rule to work. I'm sure this should be easy but I just can't get it right!
I need to add ?query=string to a specific URL pattern so:
www.example.com/downloads/file
Becomes:
www.example.com/downloads/file?query=string
The best I can come up with is:
RewriteRule ^downloads/(.*) downloads/$1?query=string
But it's not working. Where am I going wrong?
What you have looks OK, except you might need to include the L flag to prevent further rewritting:
RewriteRule ^downloads/(.*) downloads/$1?query=string [L]
And if this is intended to be an external redirect then you'll need to make the substitution absolute or root-relative and include the R flag:
RewriteRule ^downloads/(.*) /downloads/$1?query=string [R=302,L]

Shorten Long URL using htaccess file

I would like to shorten my url using htaccess
http://mydomain.com/filename/categoryname/catid/subcategoryname/subid/productname/productid/
to
http://mydomain.com/productname/
been searching for hours on how to achieve this but nothing seems to work.
Did you try mod_rewrite?
RewriteEngine on
RewriteMap shortcodes txt:/path/to/file/shortcodes.txt
RewriteRule ^/(\d+)$ ${shortcodes:$1} [R=permanent,L]
For example, in your case, this might look like this:
productname /filename/categoryname/catid/subcategoryname/subid/productname/productid/
And this should go in VirtualHost definition

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

htaccess: redirect to new url problem

I would like to redirect any php page with optional parameter to a new clean url
eg, from: account.php?id=156 to newurl/account/156
I'm using the following redirectmatch
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
but the result I get is it will redirect to newurl/account//?id=156
I thought it was funny when I read somewhere where htaccess and regular expression was compared to voodoo :) we'll now I understand why
I don't understand where your third subexpression is.
$1: ([a-zA-Z0-9_]+)
$2: ([0-9]+)
$3: MIA
May I recommend something more like this?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
then you may have to use $3 to access the id number. you want the entire parameter to be optional, right?
I never used URL redirects in .htaccess before, but if it's plain regular expressions, this should work:
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]
Since you weren't very specific in what you want, I took some guesses. This will redirect foo.php?bar=123 to newurl/foo/123 and will ignore bar.
Edit: Thinking about it, rewriting your regexp for you won't help you in the long term, and no one except you is likely to know exactly what you want. I think a better course of action is pointing you to a regexp guide. Here is one, and it's specifically targeted for mod_rewrite.
I finally found the solution. I did some more research and I used a different approach which I believe was even better then what i was using
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L]

.htaccess rewritecond rewrite case level 2 folder=info

I wanted to set .htaccess to rewrite:
example.com/bla/info/
to
example.com/info/index.php?q=bla
example.com/bla/info/txt/
to
example.com/info/index.php?q=bla&t=txt
I wanted the browser still to display:
example.com/bla/info/txt/
I didn't want rewrite in the level 2 rewrites, like:
example.com/bla/xxx/
or
example.com/ccc/zzz/aaa/
but
example.com/silly/info/
would work as well as
example.com/strange/info/mytxt/
Did this make sense?
Any help?
If you start your pattern with ^ and end it with $, the rule will only apply if the whole string matches.
RewriteRule ^/bla/info/$ /info/index.php?q=bla
RewriteRule ^/bla/info/txt/$ /info/index.php?q=bla&t=txt
If you use do not use the [R] option, the URL shown in the browser will be whatever the user entered.
Are you trying to make this general-purpose? If so, you can use regex-type matching and variable substitution. To make 'bla' match any string up to the next slash (/), use
([^/]+)
in your pattern and reference it with $1 in your substitution.
RewriteRule ^/([^/]+)/info/$ /info/index.php?q=$1
RewriteRule ^/([^/]+)/info/([^/]+)/$ /info/index.php?q=$1&t=$2
I recommend the Apache web pages about mod_rewrite for more information.
[Edit. Fixed the rules to not include the host name in the pattern, as the original poster figured out.]
--
bmb
you got me in the right track.
i ended up with something like this:
RewriteRule ^([^/\.]+)/info/?$ example/info/index.php?q=$1 [L]
thanks a lot

Resources