htaccess beginner: absolute location of links - .htaccess

I have no experience with htaccess and I'm at a loss for what I did wrong.
This works well for site.com/electronics, /index. It redirects to correspoding .php link.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^electronics/([a-zA-Z]+) electronics.php?type=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [NC,L]
However, when the url is electronics/(type) and I click on a page in the nav bar, the url becomes electronics/electronics/(type) or electronics/index, etc. I am not sure what to do, do I change the url in the nav bar to its absolute location? But, I do not know how to do that either.

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]

URL rewriting without redirect

I have a couple of directives which should redirect the user to the right path:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ user?user=$1 [QSA]
So that writing www.mywebsite.com/myprofile it displays www.mywebsite.com/user?user=myprofile
It works properly, but it redirects to that URL.
What I would like is that my browser displayed www.mywebsite.com/user?user=myprofile (which is an existing folder on my server), but still showed www.mywebsite.com/myprofile on the address bar, which is the way many websites (as far as I know) create your own profile page.
How is it possible?
you could try just as this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ user/?user=$1 [QSA]
or
RewriteRule ^([^/]+)$ user/index.php?user=$1 [QSA]
With the slash added, the URL is not redirected by only rewritten.

Possible HTACCESS issue not serving sub pages correctly

Homepage TLD resolves fine.
When clicking on subpage link e.g. "mortgages/" the subpage does not display, instead the homepage is displayed, although the pretty URL does display correctly as www.mydomain.co.uk/mortgages/
The subpage will only display if I enter the actual page "mortgages.php" into the menu structure.
So it seems that the subpages are not parsing correctly for some reason.
Could this be an HTACCESS issue?
Have tried various changes to HTACCESS
Current HTACCESS code
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
IndexIgnore *
When clicking on subpage link e.g. "mortgages/" the subpage does not display, instead the homepage is displayed, although the pretty URL does display correctly as www.mydomain.co.uk/mortgages/
I think this is what you want
remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
alternative 1
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
alternative 2
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

How to redirect complete domain to a particular folder

i want to rewrite url using .htaccess file,
my all data is saved in work folder on server
and i want to show that folder when user enter the complete webaddress
www.example.com to www.example.com/work using mod_rewrite
my code is
RewriteRule ^(.*)$ /work/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
this code create problem when i enter somthing like www.example.com/folder/ than it externally redirect me to www.example.com/work/folder/ and shows 404 page not found even when work contains that folder and that folder contain index.php
Try this :
RewriteCond %{REQUEST_URI} !^/?work/
RewriteRule ^(.*)$ /work/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]

friendly urls with .htaccess

I am using the following .htaccess code in my website to redirect all the urls to index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
then I am checking the url and see if the name of the page is available in the database. Then I am redirecting the user the that specific page if it is found like so:
www.mywebsite.com/videos.php?v=Name_of_the_video
www.mywebsite.com/images.php?i=Name_of_the_image
www.mywebsite.com/users.php?u=Name_of_the_user
as you can see I have 3 main types of pages. I want to use .htacces so I can convert those urls like so:
www.mywebsite.com/videos/Name_of_the_video
www.mywebsite.com/images/Name_of_the_image
www.mywebsite.com/users/Name_of_the_user
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])([^/]+)/(.*) /$1$2.php?$1=$3 [L,QSA]
You'll probably want to put this before your other rules though.

Resources