I have a problem with htaccess
I'm making a website for a Youtube creator and to improve my SEO I need some htaccess rule so it likes that the users vists another page (that improves Google Listing)
My website contains sections (like <section id="contact">)
I'm trying it with /contact
Rewriting website.com/#contact to website.com/contact but when I try to do so , it results in 404 (The requested URL /nieuw/contact was not found on this server.)
Currently I have a file called .htaccess (root/nieuw/.htaccess)
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^/contact$ %{HTTP_HOST}/#contact [L]
I also tried it without %{HTTP_HOST}
Directory structure:
-root
--nieuw
---index.php with sections
--oud
--.htaccess that contains `RewriteRule ^(.*)$ nieuw/$1 [L]`
(and some other stuff that isn't relevant for this question)
Can someone tell me what I do wrong and how to solve it?
Related
I currently have the following .htaccess rewrite rule setup on my site:
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.html?id=$1 [QSA]
The rule works in such a way that if I go to the following URL:
http://example.com/dashboard
It won't try and find the dashboard directory that doesn't exist but instead it will keep the URL as is and redirect the user to the root index page. From there I just use javascript to control what view the user will see depending on what path is appended.
The code works exactly as I want it to but i've now had to move my site into a sub-directory on our server. The URL structure is now this:
http://example.com/mysubdir/dashboard
I tried rewriting my rewrite rule to incorporate the directory but have not been successful so far as i'm no .htaccess expert. I tried something along the likes of:
RewriteEngine On
RewriteRule ^([^/d]+)/mysubdir/?$ index.html?id=$1 [QSA]
Could anyone tell me how I can amend my rewrite rule to work in my sub-directory?
You were close - this should do it:
RewriteEngine On
RewriteRule ^mysubdir/([^/d]+)/?$ /mysubdir/index.html?id=$1 [QSA]
Demo here: http://htaccess.mwl.be?share=6e574cc6-90a4-54ca-b113-ce72d6eb5203
When a user goes to any subdomain of my website, for example test.domain.com or even sdjfhdihdfig.domain.com, I want the page of http://www.domain.com/directory to be shown (this directory will be the same regardless of the subdomain the user goes to).
If the user heads over to any page of any subdomain (for example, test.domain.com/apage.html), I want the page of http://www.domain.com/directory/[PAGE] to be shown (in this case the page www.domain.com/directory/apage.html would be shown, but of course this would change depending on what came after the subdomain).
I found something that seems to solve this problem, however, it redirects the user rather than keeping the subdomain as it is in the URL bar. If I went to test.domain.com/test, I want this URL to stay as it is, but the page located at www.domain.com/directory/test should be served instead.
Does anyone have any ideas about how I could do this with htaccess? Thanks in advance.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www)[^.]+\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/directory/$1 [R,L]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
This assumes that mod_rewrite is both installed and activated for .htaccess files.
If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
I managed to figure out the solution to my problem eventually.
This is my .htaccess document:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)?(.+)$
RewriteRule ^$ s/$1/ [L]
When someone goes to example.domain.com, the page at http://www.domain.com/s/ is displayed. When they go to a page like example.domain.com/s/page.html, the page at http://www.domain.com/s/page.html is displayed instead.
If the user goes to www.domain.com, this is not treated as a subdomain and instead my main website homepage is displayed.
I've set up a quick script so if someone decides to go to example.domain.com/page.html, instead of showing the page from www.domain.com/page.html, they are redirected to a 404 page on my server. The script (PHP) for how to do this is below:
if(substr($_SERVER['HTTP_HOST'], 0, 4) != 'www.' AND substr($_SERVER['REQUEST_URI'], 0, 3) != '/s/' AND $_SERVER['REQUEST_URI'] != '' AND $_SERVER['REQUEST_URI'] != '/') {
header("Location: http://www.domain.com/404.php");
}
I got the htaccess file from htaccess get domain name from %{HTTP_HOST} into variable, minus the www and just adapted it a little bit.
I hope this helps someone in the future!
Here is a sample of filter URL's on an ecommerce store.
http://www.domain.com/showering/showers/filter/alliance
http://www.domain.com/showering/showers/filter/aquaflow
http://www.domain.com/showering/showers/filter/grohe
http://www.domain.com/showering/showers/filter/mira
I'm wondering if there is a way I can mask these URL's so they appear like:-
http://www.domain.com/alliance-showers
http://www.domain.com/aquaflow-showers
http://www.domain.com/grohe-showers
http://www.domain.com/mira-showers
But still display the page content from the /showering/showers/* URL's?
I then wish to be able to set the canonical URL's based on these masked URL's.
I've played around with countless variations with little success but here is something I've got so far:-
RewriteEngine on
RewriteCond %{HTTP_HOST} !^/showering/showers/filter/(alliance)
RewriteRule (.*) /alliance-showers/
When this is applied to website, all the images on the Magento store don't load incidentally along with the fact that the URL doesn't change at all.
Answer to #anubhava's comment...
.htaccess file is in root or Magento installation. It is the very first rule in file like so:-
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^-]+)-([^/]+)/?$ /showering/$2/filter/$1 [L,R]
Currently testing with this URL:-
http://www.showermania.co.uk/showering/showers/filter/alliance
Wanting to show as:-
http://www.showermania.co.uk/alliance-showers
Current answer has no affect/change on this URL at all. Thanks.
You can use a rule like this:
RewriteEngine on
RewriteRule ^showering/([^/]+)/filter/([^/]+)/?$ /$2-$1 [L,NC]
Previously I have vbulletin forum installed on main domain. Now I have replaced it with WP blog and shifted forum to subfolder. Both WP and vB has seperate htaccess files. Please help me to redirect old forum urls to new ones.
Old url pattern:
www.domain.com/f1/post-title/
www.domain.com/f2/post-title/
www.domain.com/f3/post-title/
New url pattern:
www.domain.com/forums/f1/post-title/
www.domain.com/forums/f2/post-title/
www.domain.com/forums/f3/post-title/
Please somebody help me with rewriting rules for correct redirection. Also mention which htaccess (WP or vB) to put the code. Thanks in advance.
It needs to be placed on the .htaccess on the root folder of your domain.
So if your root folder is /home/youraccount/public_html/ then in the .htaccess in that folder.
This will redirect as you asked above, any forum/topic to forums/forum/topic
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(f\d+)/([^/]+)/?$ /forums/$1/$2/ [R=301,L]
I could use ([^/]+) twice but since you mentioned you have a WordPress in the root now then you should need a more specific rule for the first folder like the above.
This will match the forum id aka f1, f2 ... up to any amount of numbers:
(f\d+)
This will get anything not a / so it will get the post id and title altogether.
([^/]+)
If you have more rules inside your .htaccess file make sure you place this rule after RewriteEngine on and before any other rule, so it doesn't conflict with other rules and redirect as you asked:
RewriteRule ^(f\d+)/([^/]+)/?$ /forums/$1/$2/ [R=301,L]
Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/