htaccess rewriting if statements - .htaccess

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]

Related

htaccess rewrite going to 404?

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]

Removing part of URL but don't redirect with htaccess

So the problem I'm looking at and haven't managed to solve!
For example the url is http://someurl.com/brands/brand
What I want to accomplish is that htaccess lets te request go to url: http://someurl.com/brands/brand but removes the "brands" part from the url.
Also if the url: http://someurl.com/brands is called the page "brands" needs to be displayed. So only when there is a brand after /brands/ it needs to do a URL rewrite to http://someurl.com/brand
I have tried this but this piece does a redirect to the new location witch doesn't exist.
RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]
So I need the above with out the redirect, it only needs to rewrite the URL.
Any help would be greatly appreciated, Thanks!
This is now my htaccess part where the rewriting is done!
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]

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.

Redirect /map/ to /file.php if /map/ does not exist using .htaccess

I'm trying to redirect users from www.hostname.com/map/ to www.hostname.com/map.php if /map/ does not exists. I have the following .htaccess file already:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Which does the following:
Redirect users automatically to the https:// variant of my website
Redirects /filename to /filename.php if there is no map called /filename/ without changing the visible url
But now I want to redirect users from /map/ to a file called /map.php as well if there is no map called /map/. Ofcourse I don't want to display the .php portion of the file name, so I basically want users to be redirected to /filename if the map /filename/ does not exists without changing the visible url. How can I rewrite the code above to make this possible?
You can use:
RewriteEngine On
RewriteBase /
# keep redirect rule before internal routing one
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# if matching .php file exists forward it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

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]

Resources