htaccess for SEO friendly urls - .htaccess

So for two days I can not understand why this won't work...:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/$ advice.php?advice=$1&id=$2
Obviously .htaccess is allowed since I am not able to directory open folders, but why doesn't the link work?

Try turning off multiviews so the server doesn't try and search for that as a file. In addition add the conditions so that it knows it's not looking for a real file or directory it's trying to serve and make the trailing slash optional.
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/?$ /advice.php?advice=$1&id=$2 [L]

Does your RewriteRule redirect to an existing file?
Try this, it works for me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /test2/test.php
http://example.com/test2/.htaccess shows the location of the .htaccess file
http://example.com/test2/test.php is a direct link
http://example.com/test2/testX4XX opens the same test.php file using rewrite

Related

.htaccess for php switch case

I am trying to make a friendly url for my website but when i use it, it always uses the current folder as rewrite and not show images, css,...
RewriteEngine On
RewriteOptions inherit
Options +FollowSymlinks
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^number/([a-z0-9]+)/?$ /index.php?id=read&num=$1 [L,NC]
and i have result as mydomain.com/number/123456/
but my page not showing with theme because all link js,css,images,... change in to /number/123456/
example: mydomain.com/number/123456/css/style.css or mydomain.com/number/123456/images/logo.png
But all that in root folder is mydomain.com/css and mydomain.com/images ~~!
How can i use with that format and my page can show correctly :(
Any solutions?
Please help me!

.htaccess variables REWRITING but not REDIRECTING

I've been looking for my answer for 2 days but since I haven't found it, I finally decided to put my question to you.
I'm using a Brinkster shared host and can access the .htaccess but no Apache configuration. Every folder on the server represents the content of one site (domain). In the root I have a .htaccess where every domain is controled with
RewriteCond %{HTTP:Host} ^(?:www\.)?domainname\.be$
RewriteRule (.*) /folder/$1 [NC,L,NS]
In the folder I have another .htacces file.
I want to rewrite and redirect for prettier URLs
http://www.domainname.com/pag.asp?name=var_name&id=var_id
should look like
http://www.domainname.com/name/
The rewrite bit works with the .htaccess code bellow.
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks -MultiViews
Options FollowSymLinks
AllowOverride All
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^name=([a-zA-Z-]+)&id=([0-9]+)$ [NC]
RewriteRule ^/?page\.asp$ /%1? [R=301,L]
</IfModule>
But for some reason www.domainname.com/name/ always gives a 404.
What am I doing wrong?
Any help wouldd be highly appreciated.
Thank you

Rewriting to hide the querystring

I've been trying to create a page that can be accessed by passing a query string to check what version should be loaded. Instead of accessing the page by using www.domain.com?version=one I'd like to be able to use www.domain.com/one and the rule should turn /one into /version=one.
Is this possible?
Something like this should work.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /?version=$1 [L,QSA]

htaccess & mod_rewrite: display dynamic php page with a folder

I would want an adress like this:
http://www.domain.com/some_page123/
to display the content of this page:
http://www.domain.com/script.php?var=some_page123
But i want the URL that appears in the browser to be www.domain.com/some_page123/
Is it possible to do that with htaccess even if the folder "some_page123" doesn't really exist ?
Also, i want to exclude the folder named "infos" from this rewriting. So if an user access the URL www.domain.com/infos/ then it will display the content of "infos" folder. I do NOT want to use script.php rewriting for the folder "infos"
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^infos/)^(.+?)/?$ /script.php?var=$1 [L,QSA,NC]
add .htacces in your root directory:
RewriteRule ^script.php - [L,NC]
RewriteRule ^infos/$ - [L,NC]
RewriteRule ^(.*)$ /script.php?var=$1
now I have tested it, it should be working.

How to redirect in .htaccess? => /ua/ to /?lang=ua

I have a multilingual website and .htaccess, which displays the all page and language ?lang=ua style.
I want to redirect (using code 301) asks site.com/en/ to site.com/?lang=en with RewriteEngine.
Example:
site.com/en/ => site.com/?lang=en
site.com/ua/news.html => site.com/news.html?lang=ua
site.com/ua/news/2-material-two.html => site.com/news/2-material-two.html?lang=ua
and so on much...
How to prepare Htaccess file for Apache to meet this criterion? And how do?
Thanks in advance
Put this code in your .htaccess file under DOCUMENT_ROOT dir:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/(.*)$ /$2?lang=$1 [L,R=301,QSA]
Try this:
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteBase /
RewriteRule http://site.com/([^/]+)/(.+).html$ http://site.com/$2.html?lang=$1 [R=301,NC]
I've just put this together off the top of my head so it may not be quite right, but the idea is that it will examine the url and the ([^/]+) should read the 1st directory, and then read everything up to the .html.
It then rewrites the url to be site.com/[everything after 1st dir up to .html].html?lang=[name of 1st dir]
Add the following to your htaccess file in the root of your domain
RewriteEngine On
RewriteBase /
#if the lang param is not present
RewriteCond %{QUERY_STRING} !^lang=
#capture the language code and redirec to url with lang param
RewriteRule ^([a-zA-Z]+)/(.+\.html)$ /$2?lang=$1 [L,R=301,NC]
Please, if you must use general "possibly directory matching"-rule, consider the !-d operator (and optional: the !-f operator).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/ http://site.com/?lang=$1 [R=301,L]
So you won't end up fixing all kinds of strange problems with existing files and/or directories.

Resources