0

I've cut and pasted so many .htaccess files my fingers are turning blue. The last few solutions I tried, redirected all of my links to index.php and I don’t want that. Since I don’t really understand what I’m doing, I can’t figure out how to fix this. I’m a web designer, not a programmer, but I prefer the clean URLs. So, I need to do the following:

  1. Redirect www and http to https;
  2. Make all URLs extensionless, without trailing slash;
  3. Redirect each .php and trailing slash to the clean URL--I have multiple PHP files, so I need a wildcard solution, not one for a specific page;
  4. I also display data pulled from a database on two different pages (view.php and edit.php). I'd like the URL to look like view/1 and edit/1 instead of view.php?id=1 and edit.php?id=1.

My current .htaccess looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

My host provides free SSL and told me to place the above code to redirect www and http to https, so I think I’m all set with #1. I now need to figure out how to implement/integrate #2, #3 and #4.

I have tried solutions at How can I make all my urls extensionless, without trailing slash. And redirect the .php and trailing slash to none? and How to redirect all my urls with no extension to end with .php and many others, but nothing gives me exactly what I need.

  • You might want to use a router in your PHP code. With the current configuration, your index.php file acts as an entry point to your app, and you can retrieve and parse the $_SERVER['REQUEST_URI'] from there. Attempting to create a website with clean urls using only mod_rewrite is an experience I had in the past and would never reiterate, because it is simply not maintainable. Plus, migrating to another webserver such as nginx would be simply impossible. See something like https://github.com/dannyvankooten/PHP-Router for an example. – SirDarius Oct 08 '18 at 15:47
  • First of all, what do your links look like? A common issue with pretty URLs is that the expectation is the wrong way around. See https://stackoverflow.com/a/20563773/476. – deceze Oct 08 '18 at 16:05
  • In the few hours since I posted, I've gained a little more understanding. I now know I have to edit my document links (e.g., change "contact.php" to "contact", etc). The link you posted was helpful. But now, @SirDarius has me concerned about doing things this way. I'll look into routers as well. Again, though, not a programmer. – girlswritecodetoo Oct 08 '18 at 19:40
  • @SirDarius, as I stated in my post, I'm not a programmer. I'm afraid the routers link you provided isn't very helpful. – girlswritecodetoo Oct 08 '18 at 20:30

1 Answers1

1

It took me 10 hours, with a short break, but I think I figured it out. It certainly seems to work so far.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^account/content$ content.php [NC,L]
RewriteRule ^account/email-reset$ email-reset.php [NC,L]
RewriteRule ^account/pwd-reset$ pwd-reset.php [NC,L]
RewriteRule ^view/(\d+)$ view.php?qdetailsid=$1 [NC,L]
RewriteRule ^edit/(\d+)$ edit.php?qdetailsid=$1 [NC,L]
RewriteRule ^delete/(\d+)$ delete.php?qdetailsid=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]
</IfModule>