rewrite url and get variable - .htaccess

How can I get the second part of a url to be a variable, rewrite it, and provide a fancy URL.
So example www.mywebsite.com/AFL would rewrite to www.mywebsite.com/index.php?league=AFL but would still display as www.mywebsite.com/AFL.
I am using the same pages for multiple leagues. When a user signs up they establish a league name in the database and then on each page I look for the league variable to display the info.
I also have many different pages. So a user could go to www.mywebsite/AFL/schedule_teamdisplay.php?Team=ABC&Year=2014. This would rewrite to www.mywebsite.com/schedule_teamdisplay.php?league=ALF&Team=ABC&Year=2014.
The league will always be the second entry in the URL.
Thanks

You can put this code in your htaccess (which has to be in your root folder)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?league=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([^/]+)/([^/]+)$ /$2?league=$1 [L,QSA]

Related

Htaccess for redirecting a directory to a file with the same name

After doing a quick lookup on how to manage sites with multiple language support, I find site.com/language/page/ the neatest url layout. (as I don't have the funds for site.language)
I have used htaccess to redirect the base site from site.com to site.com/language/
by using: RedirectMatch ^/$ /language/ where 'language' is language.html
But since I have a directory called /language/ so that all other pages in the given language can be put inside it, the site just shows up as the index of the directory.
How can I accomplish that kind of layout, if I want the main index page to show up in the url as site.com/language/ ?
Current htaccess that worked fine until I added the directory:
## Rewrite Defaults
RewriteEngine On
## Remove file extension + force trailing slash
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
## Redirect to /en-gb
RedirectMatch ^/$ /en-gb/
With the root folder looking like:
/language
stuff.html
morestuff.html
language.html
...
I would really appreciate help as this is currently a nightmare situation. When I try compiling the htaccess with answers to similar questions, everyone has just parts of what I am looking to achieve and thus I break the layout with every modification...
Is it possible to change the back-end filenames and accomplish this layout using only htaccess for front-end url rewriting since the url bar is the only thing that matters?
If I understand correctly, you should be able to solve this by removing the line:
RewriteCond %{REQUEST_FILENAME} !-d
Let me know if that works.

Mod_Rewrite for URL with multiple variables | .htaccess

I develop an e-shop site with products so for this proposal i made a product page for each of products with Mod_Rewrite for URL with the code above in my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./product.php?name=$1 [L]
So when user clicks on the link www.example.com/Nike-Air goes to Nike-Air shoes page and etc.
What i would like to do is to add one parameter more in the url, let's say size
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./product.php?name=$1&size=$2 [L]
but this parameter to not append in the url, still to displays www.example.com/Nike-Air
and also to be able to get the size data from $_GET method.
Is it possible?
Any ideas guys?

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

how joomla htaccess identifies what is the get method for particular alias?

Im developing a website which supports SEF urls. I use PHP as serverside language. I know htaccess basic codes how works with it. But the problem is if I want to rewrite a php get link I have to put each both links on htaccess like this.
RewriteRule ^sign-in$ index.php?view=signin
RewriteRule ^register$ index.php?view=register
RewriteRule ^jobs$ index.php?id=2
Is there any possible way to automate urls with htaccess and url particular alias instead of adding Rewrite rules manually? something like joomla? I was trying to understand how joomla htaccess connects with particual alias. But I still couldn't understand how it works. I cant uderstand how joomla htaccess makes relationship with article aliases. Please help. Thanks in advance.
If there isn't any difference for how an id looks like compared to how a view looks like (in your example, register is a view and jobs is an id=2), then you have to do one or the other individually:
To "automate" the views you could try just doing this:
RewriteEngine On
# all id's here:
RewriteRule ^jobs$ index.php?id=2 [L]
RewriteRule ^something-else$ index.php?id=3 [L]
RewriteRule ^another$ index.php?id=4 [L]
# this will do all views
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?view=$1 [L]
EDIT: If you need to do these mappings via an external set of aliases, you need to take a look at the RewriteMap directive. You will need to have access to the server or vhost configs in order to setup the map, but your rules can stay in an htaccess file.
Say you have a text file called "joomla_maps.txt" that looks like:
jobs id=2
another id=3
sign-in view=sign-in
register view=register
etc...
You can use that mapping by setting it up in a RewriteMap (in vhost/server config)
RewriteMap joomla txt:/path/to/joomla_maps.txt
And later in your htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?${joomla:$1} [L]
Take a look through the RewriteMap docs to get some examples of other kinds of maps, including executing a script or using a dbm hash map.

.htaccess Redirect

I need to create a redirect that sends the user to a specified php page with the variable of the page they originally requested, such as:
http://website.com/4
would send them to
http://website.com/download.php?id=4
However I don't want to redirect them if they request an actual page in the root directory, such as website.com/index.php.
Any idea how to accomplish this?
Should be exactly what you asked for; rewrites the URL to include download.php?id= unless the request is for any file that physically exists already:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ download.php?id=$1 [L]
Edit: I added the RewriteEngine On because it may not work without it depending on your server setup. To be fair, mlerley's answer reminded me that it should be there.
Assuming Apache 2.2:
RewriteEngine On
RewriteRule ^(\d+)$ /download.php?id=$1
The url doesn't change in this case. This also assumes that your id is always a number.

Resources