Using vars for specific urls in htaccess - .htaccess

I'm editing htaccess for the first time and don't know how to write proper urls redirectings.
I googled that problem, but there are just no answers for redirecting, only rewriting.
I have links like /?post=89 on my site and I wanted to redirect them to /stranica/89/
Could you possibly help me, how to use vars to catch that number and redirect correctly?
Tried suggested solution, didn't work. I'm seeking for something like this
Redirect index.php/?post=$ /stranica/$1/
but that code is not working.

If you have different querystring patterns in your site, you can try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)=(.*)
RewriteRule ^(.*).php /$1/%1/%2/? [R,L]
</IfModule>
This will redirect urls like /index.php?id=5 to /index/id/5/
The below code will redirect the exact url which you mentioned on your question
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^post=(.*)
RewriteRule ^index.php /index/stranica/%1/? [R,L]
</IfModule>

Related

.htaccess masking URL for subdomain

Our site's sub-domain reading.domain.com has the content that we want people to see but the URL should be domain.com/reading.
I'm not an expert in URL rewriting but I tried to edit htaccess, but it only redirects the page.
We're using siteground, does hosting company has something to do with their apache configuration to make this work?
Thank you very much.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^reading/$? domain.com/reading/ [R,L]
</IfModule>
I figured it out, thanks for the insight #Panama Jack, [P] = it real does need proxy, [NC] case-sensitive, [L] use this rule one time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^reading/?$ http://reading.domain.com/ [P,NC,L]
</IfModule>

301 redirect a folder but exclude child folder

I need to redirect a directory "mysite.com/blog" to a new site. However, I do NOT want to redirect a child folder "mysite.com/blog/wp-admin".
Here is my htaccess code which effectively redirects the entire /blog folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
</IfModule>
I have tried the following to exclude /wp-admin, but that didn't work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/blog/wp-admin(.*)
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
Any ideas? Thank you!
UPDATE:
I tried removing the (.*) from both lines, as directed, and its still redirecting to the new site. BUT, the URL is different. Here's the URL once it redirects:
http://www.newsite.com/newblog/?redirect_to=http%3A%2F%2Foldsite.com%2Fblog%2Fwp-admin%2F
Can you tell from this URL what's going wrong? I'm not good at regex, but it looks like it's a formatting issue.

How can I redirect old pages with question marks in the URL?

I have read about htaccess redirect and rewrite on stackoverflow and on other sites and learned how to redirect simple pages and directories, but there are about 30 links remaining that I haven't been able to redirect. The reason appears to be because they contain "?" in the link's URL. I've tried the solutions posted but I haven't been able to make enough sense of them to succeed.
These work:
Redirect /Corpfleet.php htp://www.marketyourcar.cm/wraps.php
Redirect /drivers.php htp://www.marketyourcar.cm/drivers.php
Redirect /galleries.php htp://www.marketyourcar.cm/galleries.php
These do NOT work:
Redirect /ad.php?View=FAQ htp://www.marketyourcar.cm/advertiser-faqs.php
Redirect /ad.php?View=gallery htp://www.marketyourcar.cm/galleries.php
Redirect /ad.php?View=Materials htp://www.marketyourcar.cm/products-services.php
Yes, I know that the URL above is htp and .cm - I had to break it in order to make this post with my low reputation level.
If anyone can help with this I'd appreciate it.
Thanks
If you want to redirect from like:
site.com/index.php?blabla=1&id=32
to
site.com/contact.html
then use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blabla=1&id=32
RewriteRule ^(.*)$ http://www.site.com/contact.html? [R=301,L]
</IfModule>
Redirect can't handle that. RewriteRule can. This should work.
RewriteEngine on
RewriteRule ^/ad\.php\?View\=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad\.php\?View\=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad\.php\?View\=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
Or try this:
RewriteEngine on
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad.php?View=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad.php?View=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
This might work:
Example:
RewriteEngine On
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php? [R=301,L]
Add a trailing ? to the substitution URL to remove the incoming query.

.htaccess only redirect index.php to new site

We are working on our new site, and we want to make the homepage re-direct to a subdirectory I am wondering how to do this with .htaccess as then any other page they need to go back to the old site.
These rules will forward http://yourdomain.example.com to http://yourdomain.example.com/yoursubdir/
RewriteEngine on
RewriteRule ^/?$ yoursubdir/ [L]
If you want, instead, to redirect an URL with index.php (like http://yourdomain.example.com/index.php use this rules instead.
RewriteEngine on
RewriteRule ^index.php$ yoursubdir/ [L]
In general have a look at the documentation of mod_rewrite. There are many useful and free guides around the net.
Something like this should do the trick
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/some/subdir/$1 [R=301,L]
This will rewrite the requested URI to some/subdir of newdomain.com

Htaccess URL rewrite question

I have a challenge using Apache.
In my .htaccess file I'd like to convert requests like this:
url/portfolio/filename.htm
to:
url?filename
Any takers? Thanks for your time
You have a couple of choices, depending on how you want the URL to appear to visitors (and search engines).
If you want the externally visible URL to remain url/portfolio/filename.htm, Alec's solution worked after I removed the two RewriteCond lines.
RewriteEngine On
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
If other parameters could be in the query string and you want to preserve those, add QSA to the options in brackets at the end of the rule.
If you want people outside to see url?filename instead, change the rule to:
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [L,R]
Same qualification about other query parameters applies.
If this still doesn't help, I suggest you turn on rewrite logging and look in that log for more clues. Post them here and someone will help. You might have to put this part in httpd.conf. My Apache didn't like it in .htaccess.
RewriteLog ...path...
RewriteLogLevel 3 # you'll regret anything higher
Something like this should work, although I have not tested it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
This should work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ index.php?$1 [NC,L]
or if you want to visibly change it:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]
The second one would look like example.com/?filename to the user, whereas the first one would look like example.com/portfolio/filename.htm.
Hope that helps.

Resources