I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.
Related
Hey i am in the process of making a blog for my site, i have decided to make my own rather than use a 3rd party, however i cannot figure out how to make the url's seo friendly atm they are like this /blogpost.php?id=15 however i want it to be /the-title is there any way to do this without htaccess? atm i am using this to make the urls'
echo '<a href="blogpost.php?id='.$row['id'].'">';
Then i am using _GET to get the id and then get the information from the database, i could replace id with name but that gives me /blogpost.php?title=The-Title
Thanks for the help.
In your htaccess you put something like this (if using apache)
RewriteEngine On
#ignores real files
RewriteCond %{REQUEST_FILENAME} !-f
#ignores real directorys
RewriteCond %{REQUEST_FILENAME} !-d
#ignores symlinks
RewriteCond %{REQUEST_FILENAME} !-l
#forwards the request onto index.php with the orignal querystring still intact
RewriteRule (.+) index.php?url=$1 [QSA]
Then any thing after the / will be forwared to the $_GET['url'] in index.php.
Then in index.php you can have a bootstrap class that will explode this by /.
Then you can load the appropriate class based on the first element in this array.
For example if the url was /articles
it would load the articles controller based on the /articles
Or if the url was /articles/about-something
The articles controller will load the article by fetching an article from the database with the about-something slug.
I got an problem with url rewriteing.
First of all i am starting to think that i am making mistake in the structure of my webpage.
I made more than one php file. I think that this is mistake because in all tutorials and samples urls they are using in .htaccess only index.php.
Info
1. I got in navigation tab index.php section1.php section2.php section3.php section4.php
2. In index.php i only have an basic information that won't change much.
3. In each section.php file i am echoing out list of articles.
4. each aricle got id and i am using it to echo out information when user clicks on article link.
5. link to article "http://www.example.com/section1.php?id=607575"
6. In each section.php file i got more than one page.
7. For now my links to section page looks like this "http://www.example.com/section1.php?page=1".
Questions
Qestions
Should i better echo out all information on index.php page by getting information from url?
If now my link looks like this <a href="http://www.example.com/section1.php?page=1>section1</a> do i need to manually rewrite them OR it dose not matter, .htaccess will do the job?
if i need to rewrite url will it looks like this? section1
How to rewrite urls so they look like this www.example.com/section1/page/1 and www.example.com/section1/article/607575 (i know it's bad to use only numbers, i just need to understand how it works,then i will figure it out,how to replace it with article name).
If I understand correctly, this is how you would rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /path/to/section1.php?page=$ [QSA,L]
I'm not very used to mod_rewrite but that should work. You might have to rewrite rule for ?article too.
If this was not answered correctly, please refer to http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
From: http://www.example.com/section1/page/1
To: http://www.example.com/section1.php?page=1
RewriteRule ^section(\d+)/page/(\d+)$ /path/to/section$1.php?page=$2
From: http://www.example.com/section1/article/607575
To: http://www.example.com/section1.php?id=607575
RewriteRule ^section(\d+)/article/(\d+)$ /path/to/section$1.php?id=$2
I know theres a lot of posts about redirects but this is a little different (I think).
Basically I want my outlinks to be example.com/out/1234 and I want them to go to a php that looks up the URL 1234 if referenced to in MySQL and the php header redirect to that URL.
The problem Im having is passing 1234 to a page. I know how if it was out.php?q=1234 but I want it to be /out/1234
Does there need to be an index file within an /out directory that also has a htaccess to rewrite it?
If so, any ideas what the regex need to be to do this? I have seen a few sites doing this and I cant work it out.
htaccess file in your document root, you can try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?out/(.*)$ /out.php?q=$1 [L]
Replace the /out.php with whereever your php script for handling the URL is
I want to make SEF url In that i want to remove file name also from the url.
my site is in the core php, I manage to remove the extension from the url but didn't have idea that how to remove the whole file name from the url.
As i searched on google i got this code for the .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have no idea that how to write header in php so it goes in related file but don't show the file name in url.
<a href='http://mysite.com/edit/user/2/1'>click</a>
Now if i click on above link it does goto edit.php with url "mysite.com/edit/user/2/1",
so from this how can i remove file name that is "edit".
Thanks in advance
Why? Isn't that a SEO friendly and user friendly URL? Why would you want to make it un-user fiendly? But anyway my guess is that you want /user to point to /edit by the looks of it:
RewriteRule ^/?user/*$ /edit.php?type=user&%{QUERY_STRING}
Though you will be making your site worse by using this and adding uneeded complexity to not only site maintenance but also user navigation.
Wordpress Experts:
So, my wordpress instance is currently set to use friendly urls. My current rewrite rules look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Which basically means: send everything to index.php for dispatching.
What I NEED to do, is whenever someone accesses my page /promo, I send the rest of the uri to that controller (which is a file promo.php attached to the page via Template Name: Promo) but it would still need to be piped through the index.php to do that.
In a nutshell, I want /promo/fakecompany to technically behave like this to wordpress: index.php?page=promo&fakecompany
Or even a redirect that would take the second segment of anything beyond /promo and create a querystring out of it i.e. a dynamic kind of this:
Redirect 301 /promo/fakecompany /promo/?fakecompany
Redirect 301 /promo/fakecompany/referpage /promo/?fakecompany&referpage
Is there a way to do this? Or possibly a more elegant solution? Worst comes to worse, I'm just going to have to hardcode in a redirect for the moment:
Thanks in advance.
Your current rules say : redirect anything that is not a regular file or a directory to index.php
For your request :
RewriteRule ^/promo/([^/]+)$ index.php?page=promo&$1 [L]
or
RewriteRule ^/promo/([^/]+)/([^/]+)/$ /promo/?$1&$2
Should work (provided you place it before the current rules)