.htaccess rewrite site with #item in URL for direct file access - .htaccess

I hope someone can help me, please. I created a site with ajax and I want if someone visits www.example.com/example1.html to get redirected to
www.example.com/#items/example1.htmlI need to redirect them to the /#items at the beginning of the URL for many HTML files because if someone visits the direct HTML file the site is broken (no CSS & JS is in the HTML files) because I created the HTML files only for ajax calls and want to prevent direct URL access for better user experience. I hope someone here might know how to get such a redirect using a .htaccess file.
EDIT
I tried this before with no success. I don't know how to do this redirect, to be honest :)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ /#items/$1.html

Use RewriteRule directive:
RewriteRule ^(.*).html$ /#items/$1.html [NE,R=301,L]

Related

How to redirect HTML index file to subfolder directory?

I have created a subfolder to access my HTML website. But the problem is, it is not running on the subdirectory URL but with the index.html aliases.
e.g. https://example.com/my-html-site/index.html (the website running the whole page).
But I want a simple redirection to run the website under the subdirectory like this below,
https://example.com/my-html-site/ (when I hit this URL then it shows a blank page).
Is there any way to redirect the https://example.com/my-html-site/index.html to https://example.com/my-html-site/ via htaccess file rules?
Thanks in advance,
Based on your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.html [L]

How to remove style=xx from url with .htaccess?

Could someone help me with a long time problem? Google is crawling a old style and it's messing up my page indexing.
The url is: http://www.mywebsite.com/forum/viewtopic.php?STYLE=54&f=182&t=3587
What I want to do is to remove "style=54".. is this possible? if so could someone help me with the code?
Try adding these rules to your htaccess file in your document root, above any rules you may already have:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^STYLE=[0-9]+&(.*)$
RewriteRule ^(.*)$ /$1?%1 [L,R=301]
This will redirect the browser to the same URI but with the STYLE=### part removed from the query string. So in your example, the browser would then load:
http://www.mywebsite.com/forum/viewtopic.php?f=182&t=3587

URL Redirect and Append #Hashtag

My client has gone through a rebrand and has changed their primary URL. I need to redirect all traffic that visits old URLs and send them to the same page on the new URL and ideally append a #hashtag... the hashtag will trigger an overlay explaining the redirect. Both sites are on the same Drupal codebase and thus use the same .htaccess file.
So if someone visits:
http://oldsite.com/abc/def
They should be redirected to:
http://newsite.com/abc/def/#hashtag
Any help would be greatly appreciated.
something like this?
Though I'm no expert when it comes to htaccess trickery.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com
RewriteRule (.*) http://www.newdomain.com/$1/#hashtag [R=301,L]

htaccess url'redirects and rewrites

Hope someone can help me out 'cause I utterly suck at .htaccess tweaking.
I'm developing a php based site. On this site I include specific pages based on a query string
eg: index.php?q=somePage.
In the .htaccess I look for eg: /somepage/ and tell the server to load q=somePage. When this is done the server strips all other query strings from the url ergo it does not pass them.
Now to the question, is there someway to setup the .htaccess to catch the [q] parameter and pass along all other querystrings (server side)?
eg: if I call /somepage&parameter=someparameter or /somepage/&parameter=someparameter the server will rewrite the url to /somepage/ but call the page (serverside) like so:
index.php?q=somePage&parameter=someparameter
Here's how my htaccess looks now:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?q=$1
if not I guess I'll just have to store the parameters in a session and pass 'em that way. But I hope someone can help :)
PS: If you know of a book that deals with the htaccess and can bring me from complete n00b to expert, it would be epic if you could point me in the right direction. the same actually goes for regEx ;)
Usual lot of framework move the logic of parsing the request inside the PHP
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
If the URL not identify a file or e dir, pass it to index.php
EDIT
You can see http://www.zytrax.com/tech/web/regex.htm for an introduction to regexp, but the suggest htaccess pass ALL the parameters to index PHP.
Use phpinfo(); to see how your reqest is seen by PHP
As your request here the index.php
<?php
phpinfo();
here some query you can try (I suppose your code on root of localhost):
http://localhost/
http://localhost/test
http://localhost/a/b
http://localhost/?p1=v1&p2&v2&p3&p4
http://localhost/a/b?p1=v1&p2&v2&p3&p4
http://localhost/a/b/?p1=v1&p2&v2&p3&p4

How to redirect using .htaccess using Apache?

Please help me to redirect using .htaccess like below.
http://info.domainname.com/blog/?Tag=somedynamictag
to
http://domainname.com/tag/somedynamictag
Thanks!
First of all I don't understand the purpose of rewriting http://info.domainname.com/blog/?Tag=somedynamictag to http://domainname.com/tag/somedynamictag when it had to be the other way round. People rewrite URLs to clean them (ie. remove characters like ?,&,=, etc.) but you are adding all these and making the URLs cumbersome.
I think you didn't properly understand the concept of URL rewriting. Let me explain a little.
When any URL is accessed on your website, the URL that the USER types or clicks (in your case http://domainname.com/tag/somedynamictag) is rewritten. But your question tells me that you think the other way. Your understanding is that the visitor clicks http://info.domainname.com/blog/?Tag=somedynamictag and will be rewritten to http://domainname.com/tag/somedynamictag. THIS IS WRONG!!. If you set up your website this way, each and every URL at http://domain.com/ must exist as a separate file or directory which a dynamic website like you seem to be developing is not expected to have. So I assume you have understood that you have either misformed the question or you have you have misunderstood the concept of URL rewriting. Following is the .htaccess code to redirect http://domainname.com/tag/somedynamictag to http://info.domainname.com/blog/?Tag=somedynamictag.
RewriteEngine On
RewriteCond %{HTTP_HOST} domainname.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)\/(.*)$ http://info.domainname.com/blog/?$1=$2 [L,R=301]
If you think you have correctly typed the question and understood url-rewriting right and are sure what you are trying to do is right, then here's the htaccess code (if you want to redirect http://info.domainname.com/blog/?Tag=somedynamictag to http://domainname.com/tag/somedynamictag).
RewriteEngine On
RewriteCond %{HTTP_HOST} info.domainname.com
RewriteRule ^blog\/\?([A-Za-z0-9]+)=(.*)$ http://domainname.com/$1/$2 [L,R=301]
If that doesn't work, in index.php at http://info.domainname.com/blog/, place the following code:
<?php
header("HTTP/1.1 301 moved permanently");
header("Location:http://domainname.com/blog/tag/".$_GET['Tag']);
?>
And thank you for asking this question. While answering it, I learned many things.
Hope that answers your question,
Peace...

Resources