I'm new to learning HTACCESS and I'm trying my best to work on a websites URLs to make them more "seo friendly" and load properly. I've tried searching all over for what I need to point me in the right direction, but everything seems to be for "wordpress" and this is a custom built site.
Here is my issue. This site had a previous developer, the thing was a mess, and I've spent countless hours cleaning up the site, and he built the sites to show urls like this. "www.sitename.com/about-us.php"
I'm familiar enough with htaccess to know how to remove the trailing extension for .php, even showing trailing variables properly that doesn't require the trailing slash at the end like: "sitename.com/about-us?contact=true"
I was able to get the blog view page to load with a url like this: "sitename.com/posts/post-name-here/". This one though does require the trailing slash.
What I'm trying to accomplish is making the url string to work properly with trailing variables for a few pages like, register, apply, etc. So those urls would be something like this. "sitename.com/auth/apply" or "sitename.com/auth/register"
My issue is that the urls have to have a trailing slash at the end or it doesn't load.
Plus adding a trailing variables only works when formatted like this: "sitename.com/auth/apply/?application=submitted"
I'd prefer it look more like this. "sitename.com/auth/apply?application=submitted", without the trailing slash. But I can't find how to accomplish this.
Here is my .htaccess snippet.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^posts\/(.*)/$ /view-post.php?post=$1 [QSA,NC,L]
RewriteRule ^auth\/(.*)/$ /auth-action.php?action=$1 [QSA,NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Can someone help me, or point me in the right direction?
This should be what you're looking for.
#Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) index.php [QSA,NC,L]
Try using the following in your .htaccess. This will remove the trailing slash from your URLs.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=302,L]
Make sure you clear your cache before testing this. You'll notice that I've set R=302 instead of 301. This makes it a temporary redirect for testing purposes. If you're happy with the redirect then change it to 301 to make it permanent.
Related
hoping someone can offer some assistance here. This is an issue with multiple layers. In short, I want to have pretty URLs that use a URL variable to a file within a folder.
So, I want http://www.example.com/?page=path/to/page to look like http://www.example.com/path/to/page/
On the index.php page, to load the above content:
require_once('content/'.$_GET['page'].'.php');
Current htaccess configuration:
RewriteEngine On
# Add trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
# Make URLs sexy!
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
I can get this to work with a top level page within the /content folder, however, when the php file I with to include is within a folder inside the /content folder, it does not work.
Any ideas?? I believe the issue comes down to using an addition / in the ?page= variable. htaccess doesnt seem to like that.
After some playing, I figured it out. My regex was off.
Changed:
# Make URLs sexy!
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
to:
RewriteRule ^(.*)(/)/?$ index.php?page=$1 [L]
Okay. First, I know there are multiple answers to a question like this here at StackOverflow, but they only helped me part of the way.
Unfortunately a lot of the answers are very specific and only help on the case of the original asker.
So it would be nice if I could get an explanation that might also be extendable to other users' questions (and for myself if I need to use this in other pages). That said, my issue is the following:
I've been trying to use mod_rewrite to simplify my URLs to something easier for my users to type in the address bar.
So I've got to edit .htaccess to hide .php extensions externally and edited all the links on the code to follow.
The rewrite rules I have found to work are the following:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/website/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/website/$1 [R=301,L]
# Assume php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
These only deal with external redirection, but for me I don't really think internal redirection was needed, since I'm fine with just writing the short form of the URLs.
No reason hiding the extension if you just add them back in the html forms, right?
I draw users pages by receiving the username through the GET method.
www.webpage.com/userpage.php?view=username
What I want is the URL to become
www.webpage.com/user/username
When I finally got a rule that worked for this, the CSS file wasn't loading. Images could load or not depending on the rule I was using.
I scrapped the one I wrote because it was not only doing that but also were adding paths elsewhere.
RewriteRule ^(.*[^/]) userpage.php?view=$1 [QSA,L]
Basically this rule was applying '/userpage/' to all the links on the domain, regardless of what I typed in the address bar.
And when I typed the complete to it with the userpage.php it rewrote that to userpage/userpage/user.
RewriteRule ^user/(\w+)/?$ userpage.php?view=$1
Was very similar, did the same results, but wasn't showing images either.
I know that the problem here is with relative paths, but just changing all the paths to absolute will remove all the portability from the code.
I understand that what I really need here is to make it so ONLY pages with /user/ in the path to be rewritten.
I not only would like to understand what kind of conditionals do I need to apply to the rule only change what I want, and not everything else, but I would really love if someone could tell-me if there is a way for the page to be inaccessible through the full path.
E.g. www.fullpath.com/user/username display the page, but www.fullpath.com/userpage?view=username be inaccessible.
Keep your .htaccess like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=302,L,NE]
RewriteRule ^user/([^/]+)/?$ userpage.php?view=$1 [L,QSA,NC]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Also add this in the <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's URL
I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.
Okey, so this is my problem.
I want to use mod_rewrite to make nice looking urls for my site.
I want them all to have good looking url like www.mypage/tennis or www.mypage/soccer instead of www.mypage/?page=tennis and www.mypage/?page=soccer
And with the following rules i can achive this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]
So now if I type in www.mypage/soccer my PHP script reads this url and does it's magic translating this to $_GET['page'] = soccer. All this works fine!
Problem is if I would type the URL www.mypage/soccer/ all of a sudden every linked css or image cannot be found by the website, since it now looking in the none existing folder /soccer/ off course.
How do I make a rewrite rule that transforms /soccer/ to /soccer or any other /blabla/ to /blabla
Hope my question is clear! Also if anyone have any good pages where I can learn more regular expressions i would be very happy!
Try this:
RewriteRule ^([A-Za-z0-9-]+)/?$ ?page=$1 [L]
I think you should not use mod_rewrite for this, but rather fix your CSS and image paths.
Change the paths from relative to absolute,
meaning, the paths should begin with a /.
If you have this, no matter on which site your are, /images/myimage.png will always refer to www.mypage.com/images/myimage.png.
# For URIs with query string:
RewriteCond %{QUERY_STRING} (.+)
RewriteRule (.*)/$ ?page=$1&%1 [L]
# All other
RewriteRule (.*)/$ ?page=$1 [L]