Redirecting dynamic url to simple URL - .htaccess

I'm using CodeIgniter and trying to redirect this URL
SERVERNAME/index.php/category/category?ss=ABC to SERVERNAME/ABC
What I am doing
RewriteRule ^(.*)/$ /index.php/category/category?ss=$1 [L]

You need to add a couple of conditions so that you don't blindly rewrite everything, then you need to make the slash optional and the .* non-greedy:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php/category/category?ss=$1 [L]

Related

Redirect to anothe page using .htaccess

I want to redirect the url of http://sitename/modules/fb/fb.php to http://sitename/modules/take_control .How can i do that using htaccess
I tried like
RewriteRule ^/?modules/fb/fb\.php$ take_control , Is that right ?
Your solution is the opposite of what you most likely want if you are trying to rewrite to take_control.
Give this a try. These rules should provide the rewrite with take_control as the URI both ways.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+modules/fb/fb\.php [NC]
RewriteRule ^ /take_control/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^take_control/?$ /modules/fb/fb.php [L]

Need help for .htaccess RewriteRule

I've been trying to setup a RewriteRule for my .htaccess to redirect any not found .php url to my loader.php ... For some reasons, it's redirecting even if the file exist.
Here's what I have:
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Load the url with the page loader if the url doesn't match any of the rule above
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
# Remove PHP Extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Everything is working before I added this line:
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
Right now, no matter what is in my link, even if it exist or not, it'll load the loader.php. And I have other rules like the login one, same thing but different name which is why I didn't bother posting them.
Try
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Remove PHP Extensio
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
The problem with
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
is that it accepts any .php url in the request and rewrites it to loader.php.
The condition
RewriteCond %{REQUEST_FILENAME} !-f
change this behaviour of the rule, it tells server to process the rule only when there is a request for non existent php file.

htaccess rewriting if statements

I am working on a twitter/facebook type site for a college class. Somehow they let a professor teach this class with no PHP, CSS, HTML, JavaScript,jQuery, or Ajax knowledge. I have been trying to rewrite my URLs to make them look like twitter. I have gotten all of my user profile pages to rewrite to: www.site.com/username from: www.site.com/profile.php?name=username. However, I also want to rewrite my login page, create account page, etc. Currently they are: www.site.com/login.html , www.site.com/createAccount.html. I want the to rewrite without the html. Here is my .htaccess file currently.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]
**Update
I apologize I ended up switching all of my files over to .php. But I am still having some issues. Now, I only want to redirect specific URLs. For example: www.361orc.info/login should internally redirect to www.361.orc.info/login.php . I cannot seem to figure out what is wrong with the following code. It redirects but it does it changes the client URL. I want it to just redirect internally. Here is my .htaccess file:
RewriteEngine On
#I want this code to change .com/login.php to .com/login but only internally
#the URL in the client's browser shouldn't change
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^login?$ login.php [L]
#Change the profile pages of users from .com/profile.php?name=user to .com/user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L,QSA]
you could do something like this
# Rewrite User Profiles
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^profile.php$ /%1 [R=301,L]
# Rewrite Login
RewriteRule ^login.html$ /login [R=301,L]
# Rewrite Create Account
RewriteRule ^createAccount.html$ /createAccount [R=301,L]
You are pretty close, just an external redirection rule that will redirect .html files to ones without .html extension:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ profile.php?name=$1 [L,QSA]

Redirect /about to /about.php in .htaccess

First of all, I know there are plenty of similar questions about this around, but
None of them seem to work for me
None of them actually address exactly what I want
What I want is, as the title suggests, to redirect URLs without the .php extension to the actual .php file - changing the URL if possible (which I presume is just handled by [R=301]). The latest thing I tried was this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ $1.php [R=301]
That doesn't work. I can still cant access /about.php with /about. (.htaccess rules themselves are working fine though)
I understand RegEx fine, but htaccess rules just mess with my head =[
So what should I do?
Now I know what you're thinking
One of you will say this: "Why do you want to do this? Just get rid of extensions completely and access your pages via /about or /about/ with a trailing slash."
I'd like to do that, it looks quite good. Problem is SEO - from which I assume my page ranks will get annihilated because all of a sudden they're on different URLs. So before you suggest that, suggest how I'd keep my page ranks first.
What I'm actually doing is essentially URL shortening for a poster - it's a lot easier for people to remember mywebsite.com/about than mywebsite.com/about.php.
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 /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Please Make sure you have MultiViews options disabled using: Options -MultiViews
Beware of Apaches multiviews
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Please make sure that there's mod_rewrite on your Apache HTTP Server and try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.php [R]
But clear your cache or use another browser first before checking the redirecting dynamic URLs, because you've been previously used the [R=301] flag! For more info. about that, please visit: https://stackoverflow.com/a/15999177/2007055
Could you try this one but it's quite the same as the previous code:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when it works, try adding these two conditions above the rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when any of these codes above does not work, I think there's a problem in your Apache HTTP Server.
That works for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
You can chain it if you want e.g.
RewriteEngine On
# Remove trailing slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
# Redirect to ASP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]

htaccess - URL rewrite - Remove slashes, but not from files

I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?
The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

Resources