I have a website where i do blogging... on my blog.php page i have posts from different categories.
when a user clicks on a link it is being redirect to /blog-details.php page with $_GET['title'] variable.
what I have tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
last line gives me something like
xyz.com/blog-details?title=hello-world
what i want to achieve is
xyz.com/blog/hello-world
not like
xyz.com/blog-details?title=hello-world
thank you, looking forward to answers.
An important thing you have to take into consideration when working with .htaccess files, is that you have to check if the path is a directory or file, then proceed to do your action.
I used your code and it worked properly, but simply added the -f and -d checks to make sure that this does not affect other pages.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
To test out your .htaccess file and with some dummy URLs, I'd highly recommend you to check out MadeWithLove
https://htaccess.madewithlove.be/
This will let you have a test input URL and see if you get the proper output URL (the actual URL your server would read).
With that being said, I would recommend you to look into routing for future projects and self-development. A framework I'd recommend checking out is fat-free framework (f3) for PHP. It's very easy to get into, you could learn it in 2 days, and it saves a lot of time in development - by containing routing, ORM, global variables, simplified SQL queries, and plenty of other features that essentially keep you a PHP developer, rather than a framework developer.
use below method:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/([0-9a-zA-Z_=-]+)$ blog-details.php?title=$1 [NC]
</IfModule>
Then you can access the title like $_GET['title'] on the page.
Related
One pages of site have such adds: domain.com/posts/name.php
Other pages of site have such adds: domain.com/pages/name.php
I need to cut from these adds posts/ and pages/ only.
Firstly, I tried to use in .htaccess next rules:
RewriteRule ^([A-z0-9-]+)$ /posts/$1
RewriteRule ^([A-z0-9-]+)$ /pages/$1
Don't help.
Secondly, I tried to use in .htaccess such rules and conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ posts/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/$1 [L]
As result: image of mistake 404
Also I tried to use such method:
RewriteCond %{REQUEST_URI} ^([A-z0-9-]+)$/(posts|pages)/([A-z0-9-]+)/$1 [NC]
RewriteRule .* domain.com/%1%3
Don't help....
what i am understanding you need to remove the post and pages from the url following code will help you for that.
You would need the rewrite module of Apache: mod_rewrite.
Then do something like this:
RewriteEngine on
RewriteRule ^post/(.*)$ $1
RewriteRule ^pages/(.*)$ $1
Here is the official documentation of mod_rewrite: click
please test it onece I dint test the code but did same for replacing url and its work for me.
Thanks.
The solution of this problem such:
We enter all information about the page in the MYSQL database:
image of database
We generate a page from the database:
generation of page (php)
Set the generated page for Human-Friendly Url:
file .htaccess
So everything works, and POSTS there are in their folder in the database, and PAGES in their folder.
folder in database
All is clean. If you can do better somewhere, then correct me, please.
Excellent help for my question is provided in this post. However, despite trying things as described therein I am still having problems because it seems as though my .htaccess file is not being parsed.
I have a website which consists of the following pages:
http://mywebsite.com/?menu1=home
http://mywebsite.com/?menu1=posts&menu2=johndoe
http://mywebsite.com/?menu1=posts&menu2=janedoe
http://mywebsite.com/?menu1=posts&menu2=nickdoe
http://mywebsite.com/?menu1=fashion&menu2=armani
http://mywebsite.com/?menu1=fashion&menu2=gucci
http://mywebsite.com/?menu1=about
The pages are displayed through the default index.php file in my xampp directory on Windows.
What would it take to have the browser respond to the following SEO frienly links instead? I would like to have my PHP code work with at least modifications as possible.
http://mywebsite.com/home
http://mywebsite.com/posts/johndoe
http://mywebsite.com/posts/janedoe
http://mywebsite.com/posts/nickdoe
http://mywebsite.com/fashion/armani
http://mywebsite.com/fashion/gucci
http://mywebsite.com/about
Thanks.
You can use these rules in your Root/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /?menu1=$1&menu2=$2 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?menu1=$1 [QSA,NC,L]
The first rule rewrites "/posts/jondoe/" to "/?menu=posts&menu=jonde" and the second rule rewrites "/about" to "/?home=about"..
I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.
Let say I have a site
www.mysite.com
Is there anyway to have to have user page urls be
www.mysite.com/username
instead of the usual
www.mysite.com/user.php?id=whatever
I would like to know if this is possible in php without having multiple folders and index pages. I would like to avoid the folder method because from my understanding you would have an issue with efficiency especially if you have to bounce from one user to another. I would like to do this method because it is a lot easier for someone to say get my info from
www.mysite.com/username
than the other option. Any help (tutorials, sites, etc) would be appreciated. Thanks in advance.
I would like to know if this is possible in php without having multiple folders and index pages.
Yes, its possible. You can do it redirecting all requests to your index.php and process this url (route) manually
sample .htaccess for redirecting all request to your index.php
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
BUT implemention of
www.mysite.com/username
is not easy thing. You have to know all your system usrls such as /register, /login, /post, ... etc and dont allow to register these usernames.
I recomend you implement this scheme:
www.mysite.com/~username
or
www.mysite.com/user/username
Apply front controller pattern with any kind of router [e.g. klein], then you'll need a single, universal .htaccess file in your webroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Yes sure, use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /user.php?id=$1 [L,QSA]
So i have a mvc system setup but it does not generate search engine friendly urls.
A typical url is in the format:
http://sitedomain.com/class/classMethod?parameter=valueA?parameter2=valueB
This is what i need to have:
http://sitedomain.com/class/valueA/valueB/
My .htaccess actually modified a part of the url already but i dont know how to do the second part
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
(originally looked like http://site.com/index.php?controller=class, but after the htaccess below is ran, it looks like http://site.com/class)
If anyone could help me with this, that would be great, thank you.
RewriteRule ^/class/(.*)/(.*)/$ index.php?controller=class¶meter=$1¶meter2=$2 [L,QSA]
I use the following .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ rewrite.php?%{QUERY_STRING}
And extracting parts from the url is all done in PHP.
Parsing the $_SERVER['SCRIPT_NAME'] variable.
(I find php code much easer to debug than complex apache rewrite rules.)