Replacing get variables with slashes - mod-rewrite

I want to get rid of the id's in url bar by make every thing more simple can anyone tell me how to make something like this /math/calculus/limits_topic/vidname

Use Below .htaccess file :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Pass get method variable to URL then explode it
$variables = explode("/",$_SERVER['REQUEST_URI']);
for example :
actual url : http://example.com/math.php?calculus=1&limits_topic=1&vidname=1
rewrite url : http://example.com/math/1/1/1

Related

Remove particular segment from url

I am using Codeigniter for one of my apps and have a following htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Now i have a url which is like mydomain.com/group/username
For SEO reasons i am suppose to convert this url into mydomian.com/group/username into mydomian.com/username using 301 redirect
Also urls like mydomain.com/group/username/page1 should redirect to mydomian.com/username/page
The closest i have tried after googling is by pasting this below line at the end of the file
RewriteRule ^(.*?)/?group(.*)$ /$1 [L]
But it isnt working.. any idea where i have gone wrong ?
The answers on other questions doesnt seem to be working for me/ Am pretty bad with .htaccess
P.S - i tried this
RewriteRule ^group/username(.*)$ username/$1 [QSA,R=301,L]
It works fine but it gives consecutive slashes like mydomain.com/username//pagename
I have figured it out
RewriteRule ^group/username(.*)$ username$1 [QSA,R=301,L]
This is will simply remove the 'group' from your URL and will do 301 redirect
If
RewriteRule ^group/username(.*)$ username/$1 [QSA,R=301,L]
gives two slashes, change it to:
RewriteRule ^group/username/(.*)$ username/$1 [QSA,R=301,L]
The (.*)$ matches anything after username (including the slash) until the end.

how to get from mod_rewrited url query string

I have a liitle mod_rewrite problem.
I have index.php (in root directory) which contains simple href link:
Novice
When I click on Novice, redirects me on novice.php file (which is mod_rewrited, that doesn't look like novice.php?query=this-is-something in URL, but looks like novice/this-is-something (this-is-something is my query)) The problem is, when I'm trying to GET "this-is-something" query in novice.php file.
I'm getting this query in novice.php file like that:
if (isset($_GET['query'])){
$query=$_GET['query'];
echo $query;
}else{
echo 'Null parameters.';
}
But it outputs just 0
But when I'm passing numbers in href link, like that:
Novice
The output in novice.php file is correct: 2131
I have code in my .htaccess like that:
RewriteRule ^novice/([^/\.]+)/?$ novice.php?query=$1 [L]
So what am I doing wrong in mod_rewrite, that i can get numbers through $_GET method, but I can not get string or charachters through $_GET?
I found solution by myself.
I've just changed "query" word at the end of RewriteRule ^novice/([^/.]+)/?$ novice.php?query=$1 [L]
to
"blabla" word -> RewriteRule ^novice/([^/.]+)/?$ novice.php?blabla=$1 [L]
And then works everything... Very strange...
if (isset($_GET['blabla'])){
$blabla=$_GET['blabla'];
echo $blabla;
}else{
echo 'Null parameters.';
}
It echo's now the parameter this-is-something.. But still don't know what was the problem.. I've just changed the name of variable "query" which was containing parameter "this-is-something"..
Btw if someone is interested: my .htaccess looks like this:
Options +FollowSymLinks
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/local_ageo.si/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/local_ageo.si/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteRule ^novice/([^/\.]+)/?$ novice.php?blabla=$1 [L]

Mod rewrite redirection to another domain if file not exist

What I'm trying to do:
I need to redirect a request to a file to another domain if the file not exists. For example:
http://www.mydomain.com/js/foo.js
redirects to (if not exists)
http://www.myanotherdomain.com/js/foo.js
What I do:
I wrote the next lines at the end the htaccess, but they redirect ALL!
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [L,NC]
Before these lines, I have a lot of lines like this (I'm using MVC (Model, View,Controller)):
RewriteRule ^car/brand/?$ ?controller=Car&action=viewBrand [L,NC]
What happens:
It works wells with non existing files, but seems to be imcompatible with the MVC rules. These rules have to match and then stop evaluating rules because de "L" flag. But it seems to continue evaluation of the rules and finally evaluates the redirect rule. The result is this:
http://www.mydomain.com/car/brand/
goes to
http://www.myanotherdomain.com/?controller=Car&action=viewBrand
Please can anyone help me?
Thank you very much,
Jonathan
Try this:
RewriteCond %{REQUEST_FILE} !-f
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [QSA,R,L]
See also: mod rewrite directory if file/folder not found
Try placing these rules after your MVC rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.(js|png|jpe?g|gif|css|html?)$ http://www.myanotherdomain.com/$1.$2 [L,R,NC]
If you want all requests, and not just static content like scripts and images then change the RewriteRule line to:
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [L,R,NC]

Mod_Rewrite: Transform normal /url into /#!/url

I have the following two url cases …
http://url.com/one
http://url.com/category/some
If those urls are called I want mod_rewrite to call …
http://url.com/#!/one
http://url.com/#!/category/some
How can I do that?
Something like this?
# not existing file (images, css, etc)
RewriteCond %{REQUEST_FILENAME} !-f
# no query parameters
RewriteCond %{QUERY_STRING} =""
# not /
RewriteCond %{REQUEST_URI} !^/$
# external redirect and pass along query string and uri as fragment
# i guess this must be an external redirect as the server side should
# not see the fragment, R=redirect, NE=dont escape #, L=last rule
RewriteRule ^(.*)$ /#!/$1 [R,NE,L]
# same but with query parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /?%{QUERY_STRING}#!/$1 [R,NE,L]
But im not sure if this is good idea. Maybe you should do the redirect in application logic or with a client side script instead.

Domain name in URL as variable

My .htaccess file has the following code:
RewriteEngine on
RewriteRule ^/?(.*)$ index.php?domain=$1 [L]
I'm trying to get domain names as variables from URLs like:
hxxp://www.example.com/www.domain.name or
hxxp://www.example.com/subdomain.domain.name or
hxxp://www.example.com/domain.name
but with $_GET['domain'] my variable is always 'index.php' and not the domain names.
With hxxp://www.example.com/domain/www.domain.name and .htaccess code
RewriteEngine on
RewriteRule ^domain/?(.*)$ index.php?url=$1 [L]
everything is OK, but I would like to remove the 'domain/' part from the URLs.
I've searched for this, but couldn't find anything. Could someone please help me with this?
something like
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?domain=$1 [L]
in this case $1 will be:
http://site/www.example.com $1 = www.example.com
http://site/www.example.com/xyz $1 = www.example.com/xyz
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.](\.[^/]+)+$ index.php?domain=$0 [L]
This will rewrite any request with a URL path that contains at least one dot (foo.bar, foo.bar.baz, etc.) to your index.php.

Resources