I am using this URL rewriting with PHP The Folder structure for rewriting the URLs. I am done it is working fine but after rewrite the URL the$_GET['cat_id']
is not working. How to get the data now? please help. My project is here http://199.192.21.232/~admin/category/men-items
Script
define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/' );
$rules = array(
'picture' => "/picture/(?'text'[^/]+)/(?'id'\d+)", // '/picture/some-text/51'
'album' => "/album/(?'album'[\w\-]+)", // '/album/album-slug'
'category' => "/category/(?'category'[\w\-]+)", // '/category/category-slug'
'page' => "/page/(?'page'about|contact)", // '/page/about', '/page/contact'
'post' => "/(?'post'[\w\-]+)", // '/post-slug'
'home' => "/" // '/'
);
$uri = rtrim( dirname($_SERVER["SCRIPT_NAME"]), '/' );
$uri = '/' . trim( str_replace( $uri, '', $_SERVER['REQUEST_URI'] ), '/' );
$uri = urldecode( $uri );
foreach ( $rules as $action => $rule ) {
if ( preg_match( '~^'.$rule.'$~i', $uri, $params ) ) {
include( INCLUDE_DIR . $action . '.php' );
exit();
}
}
include( INCLUDE_DIR . '404.php' );
HTaccess
RewriteEngine On
RewriteRule ^/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
In the code you posted there is no$_GET['cat_id']
variable, unless there is acat_id
URL parameter on the URL being requested (which you've not stated).
If the.htaccess
file andindex.php
script are located athttp://example.com/~admin/
(where~admin
is an Apache per-user web directory) then a request of the formhttp://example.com/~admin/category/men-items
(as in your example) would result in the$params['category']
array index holding the valuemen-items
(from the named captured group in the matching regex). If that is what you are referring to? But there is no "cat_id" here.
UPDATE:
I just want now i have two links now on my website 1:
/~admin/category.php?cat_id=2
and 2:/~admin/category/men-items
. it will create content duplicate issue in feature i want just one link like 2:/~admin/category/men-items
so need to redirect 1 link to 2
To canonicalise the URL for SEO you can do something like the following at the top of your.htaccess
file:
RewriteCond %{QUERY_STRING} ^cat_id=2$
RewriteRule ^category\.php$ /~admin/category/men-items [R=301,L]
If the old URLcategory.php
still exists as a physical file then you'll need to ensure that MultiViews is disabled in order to avoid conflicts with mod_rewrite. For example, at the very top of your.htaccess
file:
Options -MultiViews
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.