htaccess config : using part of the URL as parameter - .htaccess

I'm using a URL shortener and i'd like to have the following :
from http://domain.com/ld541as2 redirecting to http://www.newsite.com/login-ld541as2.html
There are a lot of possible URL but i'm guessing it's possible to do it with 1 rule ?
Any chance any of you guys know how to do so ?
Thanks for your help

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 ^([^/]+)/?$ /login-$1.html [L,R]
PS: If you don't want full (external) redirection then remove R flag from above rule.

Related

Url with 2 parameters / rewriting in .htaccess

I host my website on a godaddy webserver and i try to use .htaccess file to rewrite some urls but it doesn't work. Help would be very appreciated.
Basically I would like to replace an url like:
https://www.example.com/photographers/photographer.php?id=1&name=john-doe
in:
https://www.example.com/photographer/1/john-doe
Parameter 1 is: id / Parameter 2 is: name
Obviously I would like also my server to redirect internally in the opposite way.
I tried a lot of combinations but with the rewriterule but none is working. For example I tried:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^photographer/(\d+)/([A-Za-z-]+)/?$ photographers/photographer.php?id=$1&name=$2 [NC,L]
Thank you for your help!

htaccess for SEO friendly urls

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

.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]

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