SEO friendly URL not working - apache

I have a URL (Example.com/blog/) and blog is a directory in my website root. blog contains an index.php file that generates a page based on the query string of ?url=[some url] for example let's say example.com/blog/?url='hello-world' is a valid query string where information can be generated. I want that URL to be example.com/blog/hello-world
Currently, I have a global .htaccess file that removes all extensions. Inside my blog directory is another .htaccess file. It has my attempt at making the URL change to the friendly URL:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(blog)/(.*)$ blog/index.php?url=$2
Unfortunately, this isn't working. How can I solve this problem so the friendly URL displays?

Try this it like this,
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/?url=$1 [QSA,NC,L]

Related

Use ReWriteEngine to redirect user profile page to a physical page

Good day,
I want to rewrite my user id URL to a physical page on my website, see URL structure below:
From:
http://localhost:8888/report.mark1/user.php?id=341
To:
http://localhost:8888/report.mark1/341
This is the .htaccess file code:
ReWriteEngine On
RewriteRule ^report.mark1([^/.]+)?$ /user.php?id=$1 [L]
I'm I missing something?
With your shown samples, please try following htaccess rules file. Make sure your .htaccess file is present alongside with report.mark1 folder and your user.php is present inside report.mark1 folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /report.mark1/
##External redirect change of URL rules here.
RewriteCond %{THE_REQUEST} \s/(report\.mark1)/user\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to user.php rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)(\d+)/?$ /report.mark1/user.php?id=$1 [QSA,L]

Started implementing .htaccess but when navigating from the reduced URL, when going into a directory it then can't get back

So I have included an htaccess file to my server in the root directory and changed all of the ./ I could find set the absolutes.
However, when I search by URL into one of the directories pressing the home button does not take me home. Instead, it appends the index onto the end:
/website/book/index.php?p=home
Instead of
/website/index.php?p=home
Where have I made a fuddle?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/paperbound/$1 [R=301,L]
RewriteRule ^([^/\.]+)?$ index.php?p=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?p=$1&id=$2 [NC,L]
</IfModule>
Is the htaccess used. https://sitehost/website/book/2 is URL entered and page retrieved which exists as https://sitehost/website/index.php?p=book&id=2, clicking navlink to return to https://sitehost/website/index.php?p=home, instead places https://sitehost/website/book/index.php?p=home into the URL bar and returns an error as the file does not exist.
With your shown samples/attempts, please try following htaccess rules file. Make sure to keep your index.php file is present in website folder and htaccess is present along side with website folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
##Enabling rewrite engine here.
RewriteEngine ON
##Checking conditions for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##performing internal rewrite here to index.php file.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1/index.php?p=$2&id=$3 [QSA,L]

.htaccess to customize php url in folder

My php file is in this location:
https://example.com/store/store.php?id=1
and I want to rewrite it as:
https://example.com/store/store_name
I tried like below:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)/?$ store.php?id=$1 [NC,L] #Handle page requests
But it is not working for me.
With your shown samples, please try following htaccess Rules file. Make sure to place your htaccess Rules file in folder where store folder is present, place it along side with store folder NOT inside it.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/(.*)/?$ store/store.php?id=$1 [QSA,NC,L]

Redirecting all urls, including no path, to a file in subdirectory

I have checked a large amount of existing answers regarding .htaccess redirects. However none of them have helped me.
What I want to accomplish is redirecting all request urls to /api/init.php. However I've only gotten so far to where my index page www.example.com simply gives me a file listing because of the missing index.php file, while every url request with a path is working.
How can I accomplish this with .htaccess without ending up with a directory listing on my landing page?
This is as far as I got:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/init.php?path=$1 [NC,L,QSA]
Well your site root is a directory, so this rule you have excludes existing directories. What you could do is only exclude existing files, and allow existing directories to be handled by the PHP script. Like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /api/init.php?path=$1 [L,QSA]
I removed the NC flag as it's not needed. I added a condition to prevent an unnecessary file-system check.
You don't have to pass the path on in a URL parameter, as you could get it from $_SERVER['REQUEST_URI'] in PHP (not the same as REQUEST_URI in mod_rewrite, in PHP it always has the original URI). If you wanted to do that then your rule becomes nice and simple:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /api/init.php [L]
Because the query string will just be passed on unaffected (so QSA is not needed).

Redirect sitename.com/en to sitename.com/ar using htaccess file

I want to redirect sitename.com/en and all links inside that sections to the same urls with /ar instead of /en using htaccess file. The number of links inside the section is enormous, so I can't write them one by one. Please help...
Use this .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)?$ /ar/$1 [QSA,L]
If you want that old pages (en/) to no longer be used, and see the new url (ar/) in your browser, use [QSA,L,R=301] instead of [QSA,L] (it notify you're using a permanent redirection)