Seo friendly URL - .htaccess - .htaccess

In the root folder of my hosting I have an htaccess file with, among other things, the following code which redirect to https and www.:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In the /news/ folder i have entry.php file which have for example ?slug=this-is-first-entry. I want it to look like this https://www.example.com/news/this-is-first-entry.
So I want redirect this https://www.example.com/news/entry.php?slug=this-is-first-entry to this https://www.example.com/news/this-is-first-entry
I have this code in .htaccess in /news/ folder:
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
It's working fine, but redirecting https and www from root folder does not work. Please help, I am not familiar with htaccess.

This is a THUMB rule. If current directory has a .htaccess with RewriteEngine ON then it overrides parent .htaccess directives. If you want parent .htaccess rules to be applied before then use following option:
RewriteOptions InheritBefore
Before RewriteEngine On line.
So your htaccess file inside /news/ folder will become like:
RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
Additional suggestion: In case you are trying to rewrite non existing files and directories if this is the case then have your htaccess in your news folder like as follows.
RewriteOptions InheritBefore
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]

Related

RewriteRule works in .htaccess not in another included file?

I have a server with Magento and Wordpress.
Magento is in the website root /, Wordpress under /blog/.
Various Redirect 301 and RedirectMatch permanent are specified in a file in the website root. This file is "included" from the Apache vhost configuration:
Include /var/www/html/myredirects
Some pages from Wordpress should be visible from the website root, so that
http://www.mymagento.com/pretty-url/
will show the same content as
http://www.mymagento.com/blog/pretty-url/
This is currently achieved using in the .htaccess file using some rewrites as follows:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^pretty-url/ /blog/pretty-url/ [L]
[...more like the above here and the common Magento rewrites follow...]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^index.php
RewriteCond %{REQUEST_URI} !\.(html|jpg|png|gif)$
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteRule ^(.*)$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
Is it possible to move just the RewriteRule lines that show some pages from the blog in the root to the myredirects file?
I tried moving them with and without the part
Options +FollowSymLinks
RewriteEngine on
and in both cases it does not work, either a blank page or a Server Error is the result.
I cannot find in the Apache documentation what is the reason why whatever is defined in the .htaccess makes it work. Maybe it's because of the processing order?
Edit, as asked how myredirects looks like (please note these redirects work already):
They are a bunch of URLs with different product categories from Magento plus some related to the /blog/, they all look like these:
# These are Magento
RedirectMatch permanent /product-category/sub-category-1/sub-category-2/(colorA|colorB|colorC) /sub-category-2/$1
Redirect 301 /someurl/otherurl/ /another/url
# These are for Wordpress
Redirect 301 /blog/tag/some-tag/ /blog/some-tag/
Redirect 301 /blog/tag/another-tag/ /blog/another-tag/
Those for the blog are only redirecting the /tag/ part and nothing else.
And yes, it's only some of the /blog/ URLs that should be shown also in the website root, others will stay under /blog/ (I know there will be duplicate content for those shown in both paths).

Redirecting files from a directory to subdirectory causes infinite loop

Looked on here at all the answers regarding this issue but none fixes my issue. I have a directory called pdfs which used to contain all my .pdf files. They are now all inside pdfs/sales so trying to create a redirect for all files in the pdfs directory to look inside the pdfs/sales directory instead using a .htaccess file in the root of my site. Everything I've tried so far results in an infinite loop. Here's what my .htaccess looks like so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^pdfs/(.*)$ /pdfs/sales/$1 [R,NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first rule redirects all www. traffic to non-www. url. The second rule is my pdfs rule. The last rule is for redirecting all requests to index.php for seo friendly urls. Is there a conflict here? Where am I going wrong?
Thanks
You can keep your rule like this (my comment inline):
DirectoryIndex index.php
RewriteEngine on
# remove www from domain name
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/$1 [R=302,NC,L]
# for all non-files and no-directories route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]
Make sure to test it after clearing your browser cache.

redirect subdomain but keep URL intact

I am pointing blog.website.com at website.com/blog correctly however I want the URL to remain as blog.website.com
Here is my .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.website\.com$
RewriteRule ^/?$ "http\:\/\/website\.com\/blog" [L]
How can I do this?
UPDATE
I have managed to do it using the following code in my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.website\.com$
RewriteCond %{REQUEST_URI} !^/blog/$
RewriteRule (.*) http://website.com/blog/ [P]
However, now my blog images are not showing. If I put them into the blog folder created for the subdomain, they work, but I want to keep them inside the website.com/blog folder, how can I achieve this?
Try this rule instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.website\.com$
RewriteRule !^blog/ blog%{REQUEST_URI} [L]

.htaccess rewrite: redirect all URLs except home URL and assets

I would like all URLs except my root URL (/ and /index.php) and the JS/CSS and image assets it uses, to redirect to another URL. All assets are contained in the /assets directory
Here's what I have so far but it's not working
RewriteEngine on
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
RewriteCond %{REQUEST_URI} !^.assets$
RewriteCond %{REQUEST_URI} !/index.php$
RewriteCond %{REQUEST_URI} !/style.php$
RewriteCond %{REQUEST_URI} !/$
RewriteRule $ http://berlincallingny.eventbrite.com [R=302,L]
The rules I need are:
do not rewrite any URL containing "assets"
Do not rewrite index.php
Do not rewrite style.php
Do not rewrite the root URL
do not rewrite any URL containing .css or .js
Thank you!
Your regex for assets directory is not correct here:
RewriteCond %{REQUEST_URI} !^.assets$
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/assets(/.*|)$ [NC]
RewriteCond %{REQUEST_URI} !/(index|style)\.php$ [NC]
RewriteRule ^.+$ / [R=302,L]

"Pretty" URLs Not Working

I'm trying to remove the ".php" extension from the URLs of a site using this code (which I admit I copy/pasted from other questions here) in the .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Other lines of the .htaccess file do work, for instance, I have an error redirect and:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
So, I know the .htaccess file is in service in general.
I don't know what could go wrong in this area, so I'm not sure where to begin troubleshooting. Does anyone have pointers?
Thanks in advance.
Given that your domain account is /home/youraccount/public_html, your .htaccess would be inside the public_html folder with the following content:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# First we redirect the www to non-www
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/?(.*)$ http://domain.com/$1 [R=301,L]
# now we redirect phpless URLs internally to the php
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# but the file exists and ends with .php
RewriteCond %{REQUEST_FILENAME}\.php -f
# redirect to name.php
RewriteRule ^([^/]+)/?$ $1.php [L]
NOTE: If you have more rules this may conflict so I would have to look at the rest of your rule but basically the above should work as expected.
You will be able to access both:
domain.com/index
and
domain.com/index/
And it would redirect to your file index.php.

Resources