I feel like such a tool for having to post this question, but for the life of me, I cannot figure out how to resolve my issue. (I've also read/tried previous posts but none have helped me)
I'm trying to turn https://mywebsite.com/article.php?slug=pretty-url to https://mywebsite.com/article/pretty-url
The problem I'm having is the $_GET method is not recognizing the slug, so it's giving me a 404 error. The slug is definitely in my database. I'm not sure why I cannot retrieve it.
Below is my htaccess code and my php code to call the page.
Htaccess Code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule (.*) https://mywebsite.com/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mywebsite.com/$1 [L]
ErrorDocument 404 /404.php
#Pretty URL for Blog
RewriteRule ^article/([0-9a-zA-Z]+) article.php?slug=$1
#Rewrite for certain files with .php extension
RewriteRule ^about$ about.php
RewriteRule ^services$ services.php
RewriteRule ^portfolio$ portfolio.php
RewriteRule ^blogs$ blogs.php
RewriteRule ^tutorials$ tutorials.php
RewriteRule ^contact$ contact.php
RewriteRule ^privacy-policy$ privacy-policy.php
RewriteRule ^terms-of-service$ terms-of-service.php
RewriteRule ^sitemap$ sitemap.php
</IfModule>
PHP Code on the article.php page:
//Get the blog. Only blogs that are published
$slug = $_GET['slug'];
$publish = intval(1);
//Query the database
$sql = "SELECT * FROM blogs WHERE publish = $publish AND slug = $slug";
//Execute the query
$stmt = $db->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
([0-9a-zA-Z]+) will not capture pretty-url because the group doesn't allow for hyphens. Change that to ([A-Za-z0-9-]+) and add [L] to the end of that line.
Also, for the sake of doing things properly, remove the second and third calls to RewriteEngine On.
Never apologise for a question! If you're stuck, we will try to help.
What you need in your htaccess is the following:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /article.php?slug=$1 [L]
That should change your url to https://mywebsite.com/pretty-url.html
Related
Preface: Yes this question seems like duplicated, and I found related questions, but answers from there didnt help to me. :(
Hello, I want to add human readable URL's support for my PHP project. For now my URL quesry string looks like:
index.php?url=main/index
I would like to make it looks like:
index.php/main/index
I read following articles:
Stackoverflow
Cheatsheet
Stackoverflow
Stackoverflow
But when I do this:
var_dump($_GET['url']); // get empty array
, get empty array like no url parameter added.
My current .htaccess:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]
Can somebody help me please? Thanks!
URL: http://domain.com/index.php/controller/action
Rewritten URL: http://domain.com/index.php?url=controller/action
.htaccess
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index.php/(.*)$ /index.php?url=$1 [L,QSA]
Explanation:
The .* in the pattern ^index.php/(.*)$ matches everything after index.php/ on the incoming URL. The parentheses helps to capture the part as variable $1, which is then added at the end of the substitution URL /index.php?url= + $1.
[L, QSA]:
L ignore other rewrite rules, if this fits.
QSA means query string append.
You have
index.php?url=main/index
Yuo want to rewrite (and then user redirect) to this
index.php/main/index
What about trying this?
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]
R=301 or 302 depend on your need
On this example i assumed that on the original url there are only a-z chars.
Just to clarify (due the fact generally the process is inverse) on this way the users will be redirect, this rules does not convert links on your page.
In line
RewriteRule ^(.*)$ index.php?url=$1 [NC] the (.*) matches the part of the url up to '?' where the query begins.
To use the values passed in the query you need the QSA flag. Means Query String Append.
If Your URL : www.abcd.com/index.php/abcd/...
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Try the below code, Only considered for index.php you can replace it as per your requirement. Let me know if you need further assistance.
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=$2
What worked for me is:-
<?php var_dump($_GET); ?> this is the only thing I did on url http://localhost/baba.php/abcd . and original .htaccess file
DirectoryIndex baba.php
RewriteEngine ON
RewriteBase /
RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=$2
For Magento 2.4 Magento will always drop any GET parameters from the URL in a htaccess rewrite. The only way I found to make it work was to create a rewrite module. The setup is here: https://magento.stackexchange.com/a/158811/109113
For example
RewriteEngine On
RewriteRule ^tour/?$ tour-info.php [NC,L]
www.site.com/tour/ will look inside www.site.com/tour-info.php
My question is: Is it bad SEO practice to leave www.site.com/tour-info.php as an addressable url or should a redirect be placed on it?
UPDATE
If you have the htaccess code below...How can you make it more efficient using regex?
Also, in terms of SEO should accessible pages such as "tours/a.php" redirect back to "tours/a/" or to the homepage?
I am in the process of updating my website's url structure. The urls I am rewriting have not been crawled yet.
RewriteEngine on
Options -Indexes
RewriteRule ^tours/?$ tours.php
RewriteRule ^tours/a/?$ tours/a.php
RewriteRule ^tours/b/?$ tours/b.php
RewriteRule ^tours/c/?$ tours/c.php
RewriteRule ^tours/d/?$ tours/d.php
RewriteRule ^tours/e/?$ tours/e.php
RewriteRule ^tours/f/?$ tours/f.php
RewriteRule ^tours/g/?$ tours/g.php
Set a 301 redirect so anything indexing the old URL will get pointed to the new one:
RewriteCond %{THE_REQUEST} \+ /tour-info\.php(\?|\ |$)
RewriteRule ^ /tour/ [L,R=301]
I currently have it set to redirect www.foo.***/2 to www.foo.***/index.php?id=2, but i am trying to get it to redirect without displaying the URL.
I want the URL to stay as www.foo.***/2 but atually deliver www.foo.***/index.php?id=2
my htaccess file:
Options +FollowSymlinks
RewriteEngine ON
RewriteRule ^$ h**p://server/~user/folder/sub/index.php [L]
ErrorDocument 404 h**p://server/~user/folder/sub/ERROR/
ErrorDocument 500 h**p://server/~user/folder/sub/SERVER/
RewriteRule ^(error)/([0-9A-Za-z+]+)?$ h**p://server/~user/folder/sub/index.php?action=ERROR [L,NC]
RewriteRule ^(server)/([0-9A-Za-z+]+)?$ h**p://server/~user/folder/sub/index.php?action=SERVER [L,NC]
RewriteRule ^([0-9A-Za-z+]+)?$ h**p://server/~user/folder/sub/index.php?id=$1 [L,NC]
any help would be great thanks!
bo huttinger
thanks to awesome spamz protectionz, I subsituted stars for letters in the post
It was a 'same domain' issue. If you call a location on the same server, it doesn't rewrite the URL in the bar
solution: instead of using:
h**p://server/~user/folder/sub/index.php?id=$1 [L,NC]
use this:
/home/~user/folder/sub/index.php?id=$1 [L,NC]
hope this helps!
I want to direct all requests for any URL that ends with index.html to /. I have one domain on the server.
Example:
If someone wants "www.thissite.com/index.html--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/anyword/index.html"--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/folderdoesntexistonthissite/index.html"--it is directed to www.thissite.com/.
What is the .htaccess code that would enable this? (Both the rewritecondition and rewriterule)
This doesn't quite do the job:
RewriteCond %{THE_REQUEST} index\.html [NC]
RewriteRule index\.html$ http://www.thissite.com/$1 [R=301.L]
You could try this (without RewriteCond):
RewriteRule /index\.html$ http://www.thissite.com/ [R=301,NC,L]
Maybe the Error was the Period in [R=301.L].
You will need to use %{REQUEST_URI} variable to match in RewriteCond otherwise Apache strips out starting / in RewriteRule. Use below code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index.html$ [NC]
RewriteRule . / [R=301,L]
I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE
I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]