How specify more general my .htaccess routing in php? - php

I have the following rules in .htaccess:
RewriteRule js/jquery.min.js myproject/page/js/jquery.min.js
RewriteRule js/bootstrap.min.js myproject/page/js/bootstrap.min.js
RewriteRule styles/bootstrap.min.css myproject/page/styles/bootstrap.min.css
RewriteRule styles/bootstrap-theme.min.css myproject/page/styles/bootstrap-theme.min.css
RewriteRule styles/vinoservice.css myproject/page/styles/vinoservice.css
And also for images like that.
And my question is how can i specify a route like: every js/.js file serving the myproject/page/js/.js and of course for the styles also?
Thanks for the helps!
UPDATE
I tried this for them:
RewriteRule ^js\/.*(.js$) vinoservice/page/js/$1
RewriteRule ^styles\/.*(.css$) vinoservice/page/styles/$1
RewriteRule ^images\/.*(.png$) vinoservice/page/images/$1
RewriteRule ^images\/.*(.jpg$) vinoservice/page/images/$1
But not working correctly...but in regex generator says its good for them...interesting...what can be a problem? :S

You should only be concerned with the extensions if you wish to handle js/myscript.php differently, which I doubt you do.
As such, use the following:
RewriteRule js/([^/]+) myproject/page/$1 [L,R=301]
RewriteRule styles/([^/]+) myproject/page/$1 [L,R=301]

Related

I need a clean url without the parameters

Hello i need to make my url clean and i just do not know were to start as it is mind boggling, i have read numerous things in regards to clean urls but i have no idea.
This what i am getting on woorank as i am doing my seo.
Warning! We've detected parameters in a significant number of URLs.
I am unsure if this is right i have taken my real domain out and put my site instead
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^//(\.)?My site/.*$[NC]
#RewriteRule .(png|gif|jpg)$ – [F]
RewriteCond %{HTTP_HOST} !^ My site.co.uk$ [NC]
RewriteRule (.*) My site.co.uk/$1 [L,R=301]
Thank you J C
A basic htaccess can look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^mysite.co.uk [nc]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [r=301,nc]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1 [L]
For every new parameters you just add ([^/.]+)/ and extra parameter in the end..
What happens in code is up to you, but you will need a standard way of working if you want to use something like this.. .
NOTE: you can't just implement this now, because your site needs to be fully build to the structure of your htaccess.. So if you would replace this now, alot of others things might get broken soon... .
It depends. As Naruto pointed out. We need to know the structure of your code. Give some examples of how the urls are now and what you want them to look like. The examples will explain a bit how you might have programmed the website.
e.g. different php file for each page /about.php, /register.php or a single entry /index.php with every page having the same parameter keys.
/index.php?page=page1&foo=bar&qux=norf
/index.php?page=page2&foo=bar
or perhaps each page has different parameter names
/index.php?page=page1&foo=bar
/index.php?page=page2&qux=norf
What you can always do is redirect to a single index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
index.php now needs to route everything approperiately. This basically does the same as what Naruto suggests but instead in php directly and this might be easier for you.
How this routing happens depends on your code and is up to you. But let us assume that you have a different file for each page with different parameters. You could do this without changing the rest of your code.
// FROM: /shop.php?category=software&subcategory=webdevelopment
// TO: /shop/software/webdevelopment
$path = explode('/',$_SERVER['REQUEST_URI']);
if($path[0] == 'shop') {
$_GET['category'] == $path[1];
$_GET['subcategory'] == $path[2];
include('shop.php');
}
This way only one file needs to be edited and the rest of your code can still work with the $_GET. This is the same as what you would do in your htaccess.

How to change php urls on my website?

Is it possible to somehow change my website links from:
domain.com/category.php?tag=test
domain.com/section.php?tag=test
domain.com/news.php?tag=test
into this:
domain.com/category-test
domain.com/section-test
domain.com/news-test
Thanks and have a good day!
You can achieve this using .htaccess and mod_rewrite. You'll need to create a .htaccess file with the following contents:
RewriteEngine On
RewriteBase /
RewriteRule ^category-([^/]+)/?$ category.php?tag=$1
RewriteRule ^section-([^/]+)/?$ section.php?tag=$1
RewriteRule ^news-([^/]+)/?$ news.php?tag=$1
Now, accessing domain.com/category-test/ will take you to category.php?tag=test.
If you'd prefer to have slashes instead of dashes, you can use:
RewriteRule ^category/([^/]+)/?$ category.php?tag=$1
RewriteRule ^section/([^/]+)/?$ section.php?tag=$1
RewriteRule ^news/([^/]+)/?$ news.php?tag=$1
RewriteRule ^category-([a-zA-Z0-9]+)$ category.php?tag=$1 [NC,L]
RewriteRule ^section-([a-zA-Z0-9]+)$ section.php?tag=$1 [NC,L]
RewriteRule ^news-([a-zA-Z0-9]+)$ news.php?tag=$1 [NC,L]
In "category.php" file get the tag $_GET['tag'];

How do urls without get parameters work

Hi i have come across some urls of the type "http://localhost/jsfweb/cat/query/" where query is a string that will return some results from a mysql database. I am familiar with urls of the type "http://localhost/jsfweb/cat.php?query=query" how can i use those urls with php?
You can do this by rewriting url in htaccess file by this :
RewriteRule ^cat/(.*)$ cat.php?query=$1
Use modrewrite
You do something like this:
Inside a file named '.htaccess'
Options +FollowSymlinks
Options -MultiViews
RewriteEngine on
RewriteBase /php/eclipse/ShiftPlus2/
#forbidden area
#RewriteCond %{THE_REQUEST} index\.php
#RewriteRule ^index\.php$ http://localhost/php/eclipse/ShiftPlus2/? [R=301]
#unique case
RewriteRule ^email$ email.html [L]
#general case
RewriteRule ^([a-zA-Z_]*)/?$ index.php?query=$1 [L]
RewriteRule ^([a-zA-Z_]+)/([a-z]+)/?$ index.php?query=$1&action=$2 [L]
RewriteRule ^([a-zA-Z_]+)/(-?\d+)/?$ index.php?query=$1&id=$2 [L]
RewriteRule ^([a-zA-Z_]+)/([a-z]+)/(-?\d+)/?$ index.php?query=$1&action=$2$id=$3 [L]
RewriteRule ^([a-zA-Z_]+)/(\w+)/?$ index.php?query=$1&special=$2 [L]
#RewriteRule ^index.php$ login [R]
Where on the left side, there is a Rewrite rule with a regular expression and on the right, this is the real link like you know.
Take a look at Apache's mod_rewrite. For most of the websites it is done through that module. If you don't want to get your hands dirty with it, you can employ some sort of MVC framework that incorporates it.
Apache mod_rewrite is what you want. This is an excellent read for beginners:
10 Mod Rewrite Rules You Should Know
CodeIgniter (MVC framework) works by accepting a single entry point to the application, then routing according to what follows. So say you declare index.php as the default document, then /index.php/controller/view is the same as /controller/view. The parameters controller and view are used to instantiate and run the appropriate classes.

.htacces css/js subdirectory issue

I am trying to make some changes to .htaccess file of a site. I have searched all over the web, but I cant find a clear solution for that, or there is possibility that I have got it wrong. So here we are are:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/$ /e/www/ [R]
RewriteRule ^news$ /news.php
RewriteRule ^news$ /news.php
RewriteRule ^resources$ /resources.php
RewriteRule ^lexicon$ /lexicon.php
RewriteRule ^contacts$ /contacts.php
RewriteRule ^analytics$ /analytics.php
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)-(.*)$ /news.php?yy=$1&mm=$2&dd=$3&alias=$4
RewriteRule ^resources$ /contacts.php
RewriteRule ^articles_search$ /articles_search.php
RewriteRule ^articles_data_search$ /articles_data_search.php
RewriteRule ^profile/([0-9]+)&commit=(.*)$ /profile.php?id=$1&commit=$2
RewriteRule ^profile/(.*)&commit=(.*)$ /profile.php?alias=$1&commit=$2
At last 2 rows, i am trying to have a result from this:
www.example.com/profile.php?alias=kokazani&commit=Edit
to this:
www.example.com/profile/kokazani&commit=Edit
The problem now is the wrong path of css and js files. I need a solution, or a better approach of rewriting rules. I think that a solution ,maybe, is to rewrite everything like css/blabla.css to root directory/css/blabla.css, but if there a standard-solution foro this problem, i would like to know :)
Thanks in advance.
There are 2 possible (simple) solutions:
Use absolute paths for your CSS files (/include/css/style.css etc)
Use HTML's <base> element to set the correct base URL for relative URLs to follow (rather then the directory of the document which is altered by Apache).
My approach would be (as suggested by Truth) using absolute paths. If this is not possible, you could use:
RewriteRule ^(.+)/css/(.*)$ /css/$2
But be warned: This means the client (browser) will end up getting and caching multiple copies of the same file because it is fetching it (as far as it can see) from different paths. Therefore you lose the advantage of client-side stylesheet caching.

Need help with mod_rewrite

My current code is
RewriteRule ^(.*)$ index.php?pg=$1 [L,QSA]
index.php?action=product shop.com/product
index.php?action=product/add shop.com/product/add
Now I want to add paging to some of the pages, something like
index.php?action=product&page=1&show=20 shop.com/product/1/20
index.php?action=product/add&page=1&show=20 shop.com/product/add/1/20
How do I do this?
RewriteEngine On
RewriteRule ^product$ index.php?action=product
RewriteRule ^product/add$ index.php?action=product/add
RewriteRule ^product/(\d+)/(\d+)$ index.php?action=product&page=$1&show=$2
RewriteRule ^product/add/(\d+)/(\d+)$ index.php?action=product/add&page=$1&show=$2
I suggest taking a look at this answer too. I think it would be better in this case.

Categories